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

Commit2f3c19d

Browse files
add 1746
1 parent9d2b2ed commit2f3c19d

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ _If you like this project, please leave me a star._ ★
2525
|1750|[Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1750.java)||Medium|Two Pointers|
2626
|1749|[Maximum Absolute Sum of Any Subarray](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1749.java)||Medium|Greedy|
2727
|1748|[Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1748.java)||Easy|Array, HashTable|
28+
|1746|[Maximum Subarray Sum After One Operation](https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1746.java)||Medium|DP|
2829
|1745|[Palindrome Partitioning IV](https://leetcode.com/problems/palindrome-partitioning-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1745.java)||Hard|String, DP|
2930
|1743|[Restore the Array From Adjacent Pairs](https://leetcode.com/problems/restore-the-array-from-adjacent-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1743.java)||Medium|Greedy|
3031
|1742|[Maximum Number of Balls in a Box](https://leetcode.com/problems/maximum-number-of-balls-in-a-box/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1742.java)||Easy|Array|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_1746 {
4+
publicstaticclassSolution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/maximum-subarray-sum-after-one-operation/discuss/1049224/Java-O(n)-Time-O(n)-Space-DP-solution
7+
*/
8+
publicintmaxSumAfterOperation(int[]nums) {
9+
intlen =nums.length;
10+
//dp[i][0] means the sum of all elements in the subarray up to index i without any number squared
11+
//dp[i][1] means the sum of all elements in the subarray up to index i with nums[i] squared
12+
//esentially, there are three dimensions:
13+
//1. the element nums[i] squared itself might be the biggest sum of subarray itself;
14+
//2. the subarray sum without any elemtns squared + nums[i] squared
15+
//3. the subarray sum with one element prior to i square + nums[i]
16+
int[][]dp =newint[len][2];
17+
dp[0][0] =nums[0];
18+
dp[0][1] =nums[0] *nums[0];
19+
intmaxSum =dp[0][1];
20+
for (inti =1;i <len;i++) {
21+
dp[i][0] =Math.max(dp[i -1][0] +nums[i],nums[i]);
22+
dp[i][1] =Math.max(nums[i] *nums[i],Math.max(dp[i -1][0] +nums[i] *nums[i],dp[i -1][1] +nums[i]));
23+
maxSum =Math.max(maxSum,dp[i][1]);
24+
}
25+
returnmaxSum;
26+
}
27+
}
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1746;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1746Test {
10+
privatestatic_1746.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1746.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(17,solution1.maxSumAfterOperation(newint[]{2, -1, -4, -3}));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(4,solution1.maxSumAfterOperation(newint[]{1, -1,1,1, -1, -1,1}));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(1936,solution1.maxSumAfterOperation(newint[]{-44}));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(10954
35+
,solution1.maxSumAfterOperation(newint[]{29,71, -52, -23, -28,50,27,29,0,50,
36+
-92,22, -38,90,3,6,70, -56, -7,40,79,98,72,88, -5, -78,12,69,30,
37+
-73,99, -59,33,0, -6,25,87, -93,20, -89, -22,80,57,51,48,0,65, -57,
38+
-57,28, -42, -97,97, -49,38,40, -41,3,31, -12,47, -10,17, -32,68,40,
39+
55,86, -99, -2,100,89,31, -67}));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp