File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Expand file tree Collapse file tree 2 files changed +28
-0
lines changed 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+ } ;
Original file line number Diff line number Diff line change 281281997|[ Find the Town Judge] ( ./0997-find-the-town-judge.js ) |Easy|
2822821002|[ Find Common Characters] ( ./1002-find-common-characters.js ) |Easy|
2832831004|[ 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|
2842851009|[ Complement of Base 10 Integer] ( ./1009-complement-of-base-10-integer.js ) |Easy|
2852861010|[ Pairs of Songs With Total Durations Divisible by 60] ( ./1010-pairs-of-songs-with-total-durations-divisible-by-60.js ) |Medium|
2862871022|[ Sum of Root To Leaf Binary Numbers] ( ./1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
You can’t perform that action at this time.
0 commit comments