Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd05953

Browse files
refactor 1005
1 parent4ba83a3 commitcd05953

File tree

1 file changed

+6
-33
lines changed

1 file changed

+6
-33
lines changed

‎src/main/java/com/fishercoder/solutions/_1005.java

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,11 @@
33
importjava.util.Arrays;
44
importjava.util.PriorityQueue;
55

6-
/**
7-
* 1005. Maximize Sum Of Array After K Negations
8-
*
9-
* Given an array A of integers, we must modify the array in the following way:
10-
* we choose an i and replace A[i] with -A[i], and we repeat this process K times in total.
11-
* (We may choose the same index i multiple times.)
12-
*
13-
* Return the largest possible sum of the array after modifying it in this way.
14-
*
15-
* Example 1:
16-
* Input: A = [4,2,3], K = 1
17-
* Output: 5
18-
* Explanation: Choose indices (1,) and A becomes [4,-2,3].
19-
*
20-
* Example 2:
21-
* Input: A = [3,-1,0,2], K = 3
22-
* Output: 6
23-
* Explanation: Choose indices (1, 2, 2) and A becomes [3,1,0,2].
24-
* Example 3:
25-
*
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
36-
* */
376
publicclass_1005 {
387
publicstaticclassSolution1 {
39-
/**credit: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/252228/A-very-simple-java-solution*/
8+
/**
9+
* credit: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/252228/A-very-simple-java-solution
10+
*/
4011
publicintlargestSumAfterKNegations(int[]A,intK) {
4112
PriorityQueue<Integer>minHeap =newPriorityQueue<>();
4213
for (intnum :A) {
@@ -54,7 +25,9 @@ public int largestSumAfterKNegations(int[] A, int K) {
5425
}
5526

5627
publicstaticclassSolution2 {
57-
/**credit: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/252254/JavaC%2B%2BPython-Sort*/
28+
/**
29+
* credit: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/discuss/252254/JavaC%2B%2BPython-Sort
30+
*/
5831
publicintlargestSumAfterKNegations(int[]A,intK) {
5932
Arrays.sort(A);
6033
for (inti =0;i <A.length &&K >0 &&A[i] <0;i++,K--) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp