-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3196.java
24 lines (22 loc) · 883 Bytes
/
_3196.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.fishercoder.solutions.fourththousand;
public class _3196 {
public static class Solution1 {
/*
* I knew it's a DP problem, I was close to figuring out the recurrence relationship.
* <p>
* Credit: https://leetcode.com/problems/maximize-total-cost-of-alternating-subarrays/solutions/5355138/dynamic-programming-and-space-optimized-beats-100-easy-to-understand/
*/
public long maximumTotalCost(int[] nums) {
int len = nums.length;
long add = nums[0];
long subtract = nums[0];
for (int i = 1; i < len; i++) {
long newAdd = Math.max(add, subtract) + nums[i];
long newSubtract = add - nums[i];
add = newAdd;
subtract = newSubtract;
}
return Math.max(add, subtract);
}
}
}