File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 281
281
997|[ Find the Town Judge] ( ./0997-find-the-town-judge.js ) |Easy|
282
282
1002|[ Find Common Characters] ( ./1002-find-common-characters.js ) |Easy|
283
283
1004|[ Max Consecutive Ones III] ( ./1004-max-consecutive-ones-iii.js ) |Medium|
284
+ 1005|[ Maximize Sum Of Array After K Negations] ( ./1005-maximize-sum-of-array-after-k-negations.js ) |Easy|
284
285
1009|[ Complement of Base 10 Integer] ( ./1009-complement-of-base-10-integer.js ) |Easy|
285
286
1010|[ Pairs of Songs With Total Durations Divisible by 60] ( ./1010-pairs-of-songs-with-total-durations-divisible-by-60.js ) |Medium|
286
287
1022|[ Sum of Root To Leaf Binary Numbers] ( ./1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1005. Maximize Sum Of Array After K Negations
3
+ * https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given an integer array nums and an integer k, modify the array in the following way:
7
+ * - choose an index i and replace nums[i] with -nums[i].
8
+ *
9
+ * You should apply this process exactly k times. You may choose the same index i
10
+ * multiple times.
11
+ *
12
+ * Return the largest possible sum of the array after modifying it in this way.
13
+ */
14
+
15
+ /**
16
+ * @param {number[] } nums
17
+ * @param {number } k
18
+ * @return {number }
19
+ */
20
+ var largestSumAfterKNegations = function ( nums , k ) {
21
+ while ( k ) {
22
+ const i = nums . indexOf ( Math . min ( ...nums ) ) ;
23
+ nums [ i ] *= - 1 ;
24
+ k -- ;
25
+ }
26
+ return nums . reduce ( ( sum , n ) => sum + n , 0 ) ;
27
+ } ;
You can’t perform that action at this time.
0 commit comments