Skip to content

Commit 2f3c19d

Browse files
add 1746
1 parent 9d2b2ed commit 2f3c19d

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

+1
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|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1746 {
4+
public static class Solution1 {
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+
public int maxSumAfterOperation(int[] nums) {
9+
int len = 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 = new int[len][2];
17+
dp[0][0] = nums[0];
18+
dp[0][1] = nums[0] * nums[0];
19+
int maxSum = dp[0][1];
20+
for (int i = 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+
return maxSum;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1746;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1746Test {
10+
private static _1746.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1746.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(17, solution1.maxSumAfterOperation(new int[]{2, -1, -4, -3}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(4, solution1.maxSumAfterOperation(new int[]{1, -1, 1, 1, -1, -1, 1}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(1936, solution1.maxSumAfterOperation(new int[]{-44}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(10954
35+
, solution1.maxSumAfterOperation(new int[]{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)