Skip to content

Commit 2bcc4ff

Browse files
committedJan 14, 2025
Add solution #1005
1 parent b9d43d2 commit 2bcc4ff

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
997|[Find the Town Judge](./0997-find-the-town-judge.js)|Easy|
282282
1002|[Find Common Characters](./1002-find-common-characters.js)|Easy|
283283
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|
284285
1009|[Complement of Base 10 Integer](./1009-complement-of-base-10-integer.js)|Easy|
285286
1010|[Pairs of Songs With Total Durations Divisible by 60](./1010-pairs-of-songs-with-total-durations-divisible-by-60.js)|Medium|
286287
1022|[Sum of Root To Leaf Binary Numbers](./1022-sum-of-root-to-leaf-binary-numbers.js)|Easy|
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.