File tree 3 files changed +96
-2
lines changed
solution/1000-1099/1005.Maximize Sum Of Array After K Negations
3 files changed +96
-2
lines changed Original file line number Diff line number Diff line change @@ -170,7 +170,7 @@ public:
170
170
171
171
### **Go**
172
172
173
- ```cpp
173
+ ```go
174
174
func largestSumAfterKNegations(nums []int, k int) (ans int) {
175
175
cnt := map[int]int{}
176
176
for _, x := range nums {
@@ -207,6 +207,39 @@ func min(a, b int) int {
207
207
}
208
208
```
209
209
210
+ ### ** TypeScript**
211
+
212
+ ``` ts
213
+ function largestSumAfterKNegations(nums : number [], k : number ): number {
214
+ const cnt: Map <number , number > = new Map ();
215
+ for (const x of nums ) {
216
+ cnt .set (x , (cnt .get (x ) || 0 ) + 1 );
217
+ }
218
+ for (let x = - 100 ; x < 0 && k > 0 ; ++ x ) {
219
+ if (cnt .get (x )! > 0 ) {
220
+ const m = Math .min (cnt .get (x ) || 0 , k );
221
+ cnt .set (x , (cnt .get (x ) || 0 ) - m );
222
+ cnt .set (- x , (cnt .get (- x ) || 0 ) + m );
223
+ k -= m ;
224
+ }
225
+ }
226
+ if ((k & 1 ) === 1 && (cnt .get (0 ) || 0 ) === 0 ) {
227
+ for (let x = 1 ; x <= 100 ; ++ x ) {
228
+ if (cnt .get (x )! > 0 ) {
229
+ cnt .set (x , (cnt .get (x ) || 0 ) - 1 );
230
+ cnt .set (- x , (cnt .get (- x ) || 0 ) + 1 );
231
+ break ;
232
+ }
233
+ }
234
+ }
235
+ let ans = 0 ;
236
+ for (const [key, value] of cnt .entries ()) {
237
+ ans += key * value ;
238
+ }
239
+ return ans ;
240
+ }
241
+ ```
242
+
210
243
### ** ...**
211
244
212
245
```
Original file line number Diff line number Diff line change @@ -148,7 +148,7 @@ public:
148
148
149
149
### **Go**
150
150
151
- ```cpp
151
+ ```go
152
152
func largestSumAfterKNegations(nums []int, k int) (ans int) {
153
153
cnt := map[int]int{}
154
154
for _, x := range nums {
@@ -185,6 +185,39 @@ func min(a, b int) int {
185
185
}
186
186
```
187
187
188
+ ### ** TypeScript**
189
+
190
+ ``` ts
191
+ function largestSumAfterKNegations(nums : number [], k : number ): number {
192
+ const cnt: Map <number , number > = new Map ();
193
+ for (const x of nums ) {
194
+ cnt .set (x , (cnt .get (x ) || 0 ) + 1 );
195
+ }
196
+ for (let x = - 100 ; x < 0 && k > 0 ; ++ x ) {
197
+ if (cnt .get (x )! > 0 ) {
198
+ const m = Math .min (cnt .get (x ) || 0 , k );
199
+ cnt .set (x , (cnt .get (x ) || 0 ) - m );
200
+ cnt .set (- x , (cnt .get (- x ) || 0 ) + m );
201
+ k -= m ;
202
+ }
203
+ }
204
+ if ((k & 1 ) === 1 && (cnt .get (0 ) || 0 ) === 0 ) {
205
+ for (let x = 1 ; x <= 100 ; ++ x ) {
206
+ if (cnt .get (x )! > 0 ) {
207
+ cnt .set (x , (cnt .get (x ) || 0 ) - 1 );
208
+ cnt .set (- x , (cnt .get (- x ) || 0 ) + 1 );
209
+ break ;
210
+ }
211
+ }
212
+ }
213
+ let ans = 0 ;
214
+ for (const [key, value] of cnt .entries ()) {
215
+ ans += key * value ;
216
+ }
217
+ return ans ;
218
+ }
219
+ ```
220
+
188
221
### ** ...**
189
222
190
223
```
Original file line number Diff line number Diff line change
1
+ function largestSumAfterKNegations ( nums : number [ ] , k : number ) : number {
2
+ const cnt : Map < number , number > = new Map ( ) ;
3
+ for ( const x of nums ) {
4
+ cnt . set ( x , ( cnt . get ( x ) || 0 ) + 1 ) ;
5
+ }
6
+ for ( let x = - 100 ; x < 0 && k > 0 ; ++ x ) {
7
+ if ( cnt . get ( x ) ! > 0 ) {
8
+ const m = Math . min ( cnt . get ( x ) || 0 , k ) ;
9
+ cnt . set ( x , ( cnt . get ( x ) || 0 ) - m ) ;
10
+ cnt . set ( - x , ( cnt . get ( - x ) || 0 ) + m ) ;
11
+ k -= m ;
12
+ }
13
+ }
14
+ if ( ( k & 1 ) === 1 && ( cnt . get ( 0 ) || 0 ) === 0 ) {
15
+ for ( let x = 1 ; x <= 100 ; ++ x ) {
16
+ if ( cnt . get ( x ) ! > 0 ) {
17
+ cnt . set ( x , ( cnt . get ( x ) || 0 ) - 1 ) ;
18
+ cnt . set ( - x , ( cnt . get ( - x ) || 0 ) + 1 ) ;
19
+ break ;
20
+ }
21
+ }
22
+ }
23
+ let ans = 0 ;
24
+ for ( const [ key , value ] of cnt . entries ( ) ) {
25
+ ans += key * value ;
26
+ }
27
+ return ans ;
28
+ }
You can’t perform that action at this time.
0 commit comments