8000 add 3196 · githubniraj/Leetcode@c1448d7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1448d7

Browse files
add 3196
1 parent f936bb2 commit c1448d7

File tree

3 files changed

+52
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+52
-0
lines changed

paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3196 | [Maximize Total Cost of Alternating Subarrays](https://leetcode.com/problems/maximize-total-cost-of-alternating-subarrays/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3196.java) | | Medium |DP
34
| 3195 | [Find the Minimum Area to Cover All Ones I](https://leetcode.com/problems/find-the-minimum-area-to-cover-all-ones-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3195.java) | | Medium |
45
| 3194 | [Minimum Average of Smallest and Largest Elements](https://leetcode 10000 .com/problems/minimum-average-of-smallest-and-largest-elements/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3194.java) | | Easy |
56
| 3192 | [Minimum Operations to Make Binary Array Elements Equal to One II](https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3192.java) | | Medium |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3196 {
4+
public static class Solution1 {
5+
/**
6+
* I knew it's a DP problem, I was close to figuring out the recurrence relationship.
7+
* <p>
8+
* Credit: https://leetcode.com/problems/maximize-total-cost-of-alternating-subarrays/solutions/5355138/dynamic-programming-and-space-optimized-beats-100-easy-to-understand/
9+
*/
10+
public long maximumTotalCost(int[] nums) {
11+
int len = nums.length;
12+
long add = nums[0];
13+
long subtract = nums[0];
14+
for (int i = 1; i < len; i++) {
15+
long newAdd = Math.max(add, subtract) + nums[i];
16+
long newSubtract = add - nums[i];
17+
18+
add = newAdd;
19+
subtract = newSubtract;
20+
}
21+
return Math.max(add, subtract);
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3196;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3196Test {
10+
private static _3196.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3196.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(10, solution1.maximumTotalCost(new int[]{1, -2, 3, 4}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(-7, solution1.maximumTotalCost(new int[]{-14, -13, -20}));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)
0