Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 40c107d

Browse files
committed
1005 accepted. 4ms
1 parent 1d37dd7 commit 40c107d

File tree

4 files changed

+168
-19
lines changed

4 files changed

+168
-19
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [1005. Maximize Sum Of Array After K Negations](https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/)
2+
3+
Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total. (We may choose the same index i multiple times.)
4+
5+
Return the largest possible sum of the array after modifying it in this way.
6+
7+
Example 1:
8+
9+
```text
10+
Input: A = [4,2,3], K = 1
11+
Output: 5
12+
Explanation: Choose indices (1,) and A becomes [4,-2,3].
13+
```
14+
15+
Example 2:
16+
17+
```text
18+
Input: A = [3,-1,0,2], K = 3
19+
Output: 6
20+
Explanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2].
21+
```
22+
23+
Example 3:
24+
25+
```text
26+
Input: A = [2,-3,-1,5,-4], K = 2
27+
Output: 13
28+
Explanation: Choose indices (1, 4) and A becomes [2,3,-1,5,4].
29+
```
30+
31+
Note:
32+
33+
- 1 <= A.length <= 10000
34+
- 1 <= K <= 10000
35+
- -100 <= A[i] <= 100
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problem1005
2+
3+
import "container/heap"
4+
5+
func largestSumAfterKNegations(A []int, K int) int {
6+
h := intHeap(A)
7+
heap.Init(&h)
8+
9+
for K > 0 {
10+
h[0] = -h[0]
11+
heap.Fix(&h, 0)
12+
K--
13+
}
14+
15+
sum := 0
16+
for i := 0; i < len(h); i++ {
17+
sum += h[i]
18+
}
19+
return sum
20+
}
21+
22+
// intHeap 实现了 heap 的接口
23+
type intHeap []int
24+
25+
func (h intHeap) Len() int {
26+
return len(h)
27+
}
28+
29+
func (h intHeap) Less(i, j int) bool {
30+
return h[i] < h[j]
31+
}
32+
33+
func (h intHeap) Swap(i, j int) {
34+
h[i], h[j] = h[j], h[i]
35+
}
36+
37+
func (h *intHeap) Push(x interface{}) {
38+
*h = append(*h, x.(int))
39+
}
40+
41+
func (h *intHeap) Pop() interface{} {
42+
res := (*h)[len(*h)-1]
43+
*h = (*h)[:len(*h)-1]
44+
return res
45+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package problem1005
2+
3+
import (
4+
"container/heap"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
A []int
13+
K int
14+
ans int
15+
}{
16+
17+
{
18+
[]int{-75, 44, -14, -80, 11, 81, 67, 73, 75, -77, -35, -77, 9, 22, 11, 93, -34, 5, 78, 80, 1, 0, 94, 56, 21, -79, 13, -54, 82, 19, -99, -19, 6, -18, 39, -19, 83, 96, -66, -75, -3, 54, 97, -4, 77, -66, 69, 14, 17, -59, 13, -32, -6, -52, 74, 73, 61, -26, -62, 89, 71, 22, -35, -61, -75, -10, 17, -54, -49, -78, 79, -79, 63, 27, 85, -66, -24, 2, -15, -42, -58, 60, -8, 6, 43, -33, -11, -52, -62, -79, 93, -78, -36, -80, -23, -72, 43, -96, -25, 60, 49, -48, 44, 54, 11, 13, 82, 73, 17, -22, -9, -16, -83, 23, 6, 83, 37, -93, -43, 5, 9, -25, -87, -69, -51, -10, 36, 77, 62, -92, 100, 13, 64, 84, 100, 30, 88, 50, -56, -55, 50, 57, 77, -40, 15, -25, 8, -87, 50, 13, 45, -49, -66, -65, -6, -90, -82, 68, -86, -25, 2, 72, 87, 69, 57, -43, -32, 54, -69, -42, 21, 0, -4, 82, 44, 84, -34, -44, 88, 92, 30, 66, 80, -68, 10, -15, -8, -87, -37, -49, 52, -59, 53, 21, -76, 32, -74, 87, 89, 58, 50, -47, 14, 64, 36, -84, 57, 58, -49, -15, -96, -64, 33, -100, -89, 76, -20, -79, -35, 6, 37, -90, 28, -14, 32, 16, -88, -32, -59, 40, -7, -51, 99, -31, -99, -100, -5, -19, 59, 83, -72, 59, -59, 50, 67, 56, 76, 67, -47, -16, -99, -87, -27, 80, -9, 45, 41, -42, 64, -28, 65, 56, 65, 27, -22, 37, -70, -35, -74, -69, 12, -37, 3, -76, 70, -100, 19, -3, -69, -15, -29, 98, -16, 19, -59, 38, -3, -39, -61, -26, -6, -73, -80, 41, -79, 42, 34, 98, -68, 97, 10, -16, 92, -21, 100, -75, 1, -74, 61, 41, 58, -96, -4, -93, -34, -32, 94, 60, 57, -56, -17, 5, 83, 22, -40, 51, 0, -61, 62, 25, -32, -56, -98, -55, -96, -92, 100, -50, 66, 7, 46, 23, -53, -41, -85, -71, 58, -47, -20, 9, -24, 36, 19, 22, -78, 56, -38, 53, -10, -72, 11, -92, 90, -45, -80, 100, -98, -85, 45, -18, -37, -15, -56, 28, -2, -11, 55, 18, 65, 18, -19, -43, 39, 31, -76, 89, -1, -97, 25, -96, -26, 34, 70, -80, -9, -97, -41, 43, 96, 82, 95, -67, -22, 60, 95, -41, -90, -34, 25, -35, 76, -26, -46, -20, 63, -41, -46, -6, -91, 8, -43, 93, -54, 80, -93, 41, -19, 63, 66, 47, 50, -3, -16, 46, -28, -100, -27, -76, -93, 19, 56, 64, 36, 53, -99, -5, -75, -91, -37, 78, -49, -92, -43, 8, -22, -90, -65, -55, 21, -13, 98, 38, 93, 65, -54, -77, -29, -50, 15, -4, 22, 17, -65, 17, 55, 74, 45, 78, -12, 20, -81, -72, 3, 19, -13, -93, 50, 21, -67, 17, 42, 99, 34, -53, -85, -40, -54, 92, -35, 10, 73, 9, 0, 56, -40, 0, -44, -2, -50, -33, 42, 64, -65, 92, 78, 42, 40, 41, 84, 27, 78, -59, -24, 42, 47, -24, 66, -1, -15, 32, -23, -90, -89, -38, 17, -82, -76, 43, 89, 37, 48, -13, 54, 64, -21, -27, 74, -4, 30, 19, -74, 42, 18, 64, 39, 29, 45, 15, -43, 13, 9, -39, -46, -92, 91, -23, -55, 63, 40, -12, -5, 80, -39, -80, 16, -78, 91, 75, -46, 25, 19, 71, 17, -32, -61, -65, -24, -94, 67, 45, 73, 89, 50, 2, 19, -33, -30, 14, -47, 67, -67, 59, -90, -58, -57, -37, 14, 22, 10, -82, -14, 82, 88, -41, 26, 61, 87, 66, 36, 93, -15, -96, -24, -3, 68, -58, -63, -58, -75, 82, -98, -50, 7, -29, 87, -5, 80, -23, -79, -64, -97, -61, -3, 50, -93, -82, -63, -13, -16, 40, -25, 28, -89, 55, -17, -22, 66, 26, 83, 31, 71, -66, -60, -69, -60, -54, 28, 87, 39, 87, 89, 32, -37, 10, 30, -69, -9, 29, 32, 89, 100, 62, 73, 26, -61, -70, 90, -34, 54, -49, -88, 60, -99, 78, 37, 98, 6, 75, 95, -91, 89, 22, -31, -49, 65, 11, 9, -94, 17, -25, -25, -79, 13, -8, -86, -92, 0, 21, -34, 36, 58, 94, 75, 82, 10, 83, -57, 48, 69, 34, -46, 24, -1, -19, -48, -82, -95, -54, 86, 90, -91, 52, -63, 50, 87, 100, 48, -33, 76, -77, -23, -86, 84, 38, -86, -84, 24, -32, 52, -10, -71, 9, -30, -82, 65, 79, 38, 95, 2, 86, 95, -56, 74, 87, 48, -28, -92, -9, 70, -75, 49, -84, 37, -25, -32, 93, 88, -63, -80, 96, -14, 13, -8, -40, 60, 14, -28, 98, -1, -34, 51, 23, 74, -85, 53, 62, -67, 80, 30, -22, 41, 93, -58, -62, -35, -79, 22, -76, -100, -38, -45, 15, -50, 43, -30, 44, -27, 55, -70, -80, 67, 2, 48, 45, 49, -61, -4, -89, -90, 84, -47, -8, 62, 61, 21, 31, 94, 40, 20, 98, -73, 46, -64, -61, -69, -44, -84, 24, 83, -93, -6, -48, -75, -20, 92, -28, -55, 24, -18, -9, 31, -84, -100, 89, 50, -36, -5, 67, -67, 20, -90, -33, -29, 53, 77, -19, 68, -51, -30, -41, 22, 91, 7, -74, 38, -39, 71, 6, 73, 50, 44, 85, 9, 74, -29, -27, -39, -6, 29, 83, -18, -42, 91, 69, 58, 3, -60, 93, -39, -21, -77, 7, 12, -81, -99, 94, 12, -6, -82, 99, -31, -15, 0, -57, -100, 1, 54, -19, 47, 4, 39, -63, 38, -85, 20, 62, 47, -24, -74, -73, 17, -89, -4, 49, 71, -83, 88, 36, 11, 100, -71, 35, -46, -74, 11, 47, -96, 82, 45, 69, -25, 86, -27, -33, 25, -86, 91, 10, 62, 7, -56, 38, 86, 62, -28, -31, 15, -17, -100, -61, 96, 61, 80, 78, -87, -13, -59, -79, 62, 35},
19+
848,
20+
50592,
21+
},
22+
23+
{
24+
[]int{4, 2, 3},
25+
1,
26+
5,
27+
},
28+
29+
{
30+
[]int{3, -1, 0, 2},
31+
3,
32+
6,
33+
},
34+
35+
{
36+
[]int{2, -3, -1, 5, -4},
37+
2,
38+
13,
39+
},
40+
41+
// 可以有多个 testcase
42+
}
43+
44+
func Test_largestSumAfterKNegations(t *testing.T) {
45+
ast := assert.New(t)
46+
47+
for _, tc := range tcs {
48+
ast.Equal(tc.ans, largestSumAfterKNegations(tc.A, tc.K), "输入:%v", tc)
49+
}
50+
}
51+
52+
func Benchmark_largestSumAfterKNegations(b *testing.B) {
53+
for i := 0; i < b.N; i++ {
54+
for _, tc := range tcs {
55+
largestSumAfterKNegations(tc.A, tc.K)
56+
}
57+
}
58+
}
59+
60+
func Test_intHeap(t *testing.T) {
61+
ast := assert.New(t)
62+
//
63+
var h intHeap
64+
heap.Init(&h)
65+
expected := 1
66+
heap.Push(&h, expected)
67+
actual := heap.Pop(&h).(int)
68+
ast.Equal(expected, actual)
69+
}

leetcode.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
33
"Ranking": 887,
4-
"Updated": "2019-05-12T22:02:29.493871364+08:00",
4+
"Updated": "2019-05-13T19:30:45.853143258+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 231,
@@ -637,7 +637,7 @@
637637
"ID": 51,
638638
"Title": "N-Queens",
639639
"TitleSlug": "n-queens",
640-
"PassRate": "38%",
640+
"PassRate": "39%",
641641
"Difficulty": "Hard",
642642
"IsAccepted": true,
643643
"IsPaid": false,
@@ -4381,7 +4381,7 @@
43814381
"ID": 363,
43824382
"Title": "Max Sum of Rectangle No Larger Than K",
43834383
"TitleSlug": "max-sum-of-rectangle-no-larger-than-k",
4384-
"PassRate": "34%",
4384+
"PassRate": "35%",
43854385
"Difficulty": "Hard",
43864386
"IsAccepted": true,
43874387
"IsPaid": false,
@@ -6577,7 +6577,7 @@
65776577
"ID": 546,
65786578
"Title": "Remove Boxes",
65796579
"TitleSlug": "remove-boxes",
6580-
"PassRate": "37%",
6580+
"PassRate": "38%",
65816581
"Difficulty": "Hard",
65826582
"IsAccepted": true,
65836583
"IsPaid": false,
@@ -7081,7 +7081,7 @@
70817081
"ID": 588,
70827082
"Title": "Design In-Memory File System",
70837083
"TitleSlug": "design-in-memory-file-system",
7084-
"PassRate": "39%",
7084+
"PassRate": "38%",
70857085
"Difficulty": "Hard",
70867086
"IsAccepted": false,
70877087
"IsPaid": true,
@@ -7417,7 +7417,7 @@
74177417
"ID": 616,
74187418
"Title": "Add Bold Tag in String",
74197419
"TitleSlug": "add-bold-tag-in-string",
7420-
"PassRate": "38%",
7420+
"PassRate": "39%",
74217421
"Difficulty": "Medium",
74227422
"IsAccepted": false,
74237423
"IsPaid": true,
@@ -7693,7 +7693,7 @@
76937693
"ID": 639,
76947694
"Title": "Decode Ways II",
76957695
"TitleSlug": "decode-ways-ii",
7696-
"PassRate": "25%",
7696+
"PassRate": "24%",
76977697
"Difficulty": "Hard",
76987698
"IsAccepted": true,
76997699
"IsPaid": false,
@@ -8365,7 +8365,7 @@
83658365
"ID": 695,
83668366
"Title": "Max Area of Island",
83678367
"TitleSlug": "max-area-of-island",
8368-
"PassRate": "56%",
8368+
"PassRate": "57%",
83698369
"Difficulty": "Medium",
83708370
"IsAccepted": true,
83718371
"IsPaid": false,
@@ -8677,7 +8677,7 @@
86778677
"ID": 721,
86788678
"Title": "Accounts Merge",
86798679
"TitleSlug": "accounts-merge",
8680-
"PassRate": "39%",
8680+
"PassRate": "40%",
86818681
"Difficulty": "Medium",
86828682
"IsAccepted": true,
86838683
"IsPaid": false,
@@ -9301,7 +9301,7 @@
93019301
"ID": 773,
93029302
"Title": "Sliding Puzzle",
93039303
"TitleSlug": "sliding-puzzle",
9304-
"PassRate": "51%",
9304+
"PassRate": "52%",
93059305
"Difficulty": "Hard",
93069306
"IsAccepted": true,
93079307
"IsPaid": false,
@@ -9601,7 +9601,7 @@
96019601
"ID": 798,
96029602
"Title": "Smallest Rotation with Highest Score",
96039603
"TitleSlug": "smallest-rotation-with-highest-score",
9604-
"PassRate": "40%",
9604+
"PassRate": "39%",
96059605
"Difficulty": "Hard",
96069606
"IsAccepted": true,
96079607
"IsPaid": false,
@@ -10417,7 +10417,7 @@
1041710417
"ID": 866,
1041810418
"Title": "Prime Palindrome",
1041910419
"TitleSlug": "prime-palindrome",
10420-
"PassRate": "19%",
10420+
"PassRate": "20%",
1042110421
"Difficulty": "Medium",
1042210422
"IsAccepted": true,
1042310423
"IsPaid": false,
@@ -12289,7 +12289,7 @@
1228912289
"ID": 1022,
1229012290
"Title": "Sum of Root To Leaf Binary Numbers",
1229112291
"TitleSlug": "sum-of-root-to-leaf-binary-numbers",
12292-
"PassRate": "50%",
12292+
"PassRate": "51%",
1229312293
"Difficulty": "Easy",
1229412294
"IsAccepted": false,
1229512295
"IsPaid": false,
@@ -12457,7 +12457,7 @@
1245712457
"ID": 1036,
1245812458
"Title": "Escape a Large Maze",
1245912459
"TitleSlug": "escape-a-large-maze",
12460-
"PassRate": "39%",
12460+
"PassRate": "38%",
1246112461
"Difficulty": "Hard",
1246212462
"IsAccepted": false,
1246312463
"IsPaid": false,
@@ -12469,7 +12469,7 @@
1246912469
"ID": 1037,
1247012470
"Title": "Valid Boomerang",
1247112471
"TitleSlug": "valid-boomerang",
12472-
"PassRate": "36%",
12472+
"PassRate": "37%",
1247312473
"Difficulty": "Easy",
1247412474
"IsAccepted": false,
1247512475
"IsPaid": false,
@@ -12517,7 +12517,7 @@
1251712517
"ID": 1041,
1251812518
"Title": "Robot Bounded In Circle",
1251912519
"TitleSlug": "robot-bounded-in-circle",
12520-
"PassRate": "36%",
12520+
"PassRate": "39%",
1252112521
"Difficulty": "Easy",
1252212522
"IsAccepted": false,
1252312523
"IsPaid": false,
@@ -12529,7 +12529,7 @@
1252912529
"ID": 1042,
1253012530
"Title": "Flower Planting With No Adjacent",
1253112531
"TitleSlug": "flower-planting-with-no-adjacent",
12532-
"PassRate": "40%",
12532+
"PassRate": "42%",
1253312533
"Difficulty": "Easy",
1253412534
"IsAccepted": false,
1253512535
"IsPaid": false,
@@ -12541,7 +12541,7 @@
1254112541
"ID": 1043,
1254212542
"Title": "Partition Array for Maximum Sum",
1254312543
"TitleSlug": "partition-array-for-maximum-sum",
12544-
"PassRate": "52%",
12544+
"PassRate": "54%",
1254512545
"Difficulty": "Medium",
1254612546
"IsAccepted": false,
1254712547
"IsPaid": false,
@@ -12553,7 +12553,7 @@
1255312553
"ID": 1044,
1255412554
"Title": "Longest Duplicate Substring",
1255512555
"TitleSlug": "longest-duplicate-substring",
12556-
"PassRate": "15%",
12556+
"PassRate": "21%",
1255712557
"Difficulty": "Hard",
1255812558
"IsAccepted": false,
1255912559
"IsPaid": false,

0 commit comments

Comments
 (0)