A subarray of a 0-indexed integer array is a contiguous non-empty sequence of elements within an array.
The alternating subarray sum of a subarray that ranges from index i to
j (inclusive , 0 <= i <= j < nums.length) is nums[i] - nums[i+1] + nums[i+2] - ... +/- nums[j].
Given a 0-indexed integer array nums, return _themaximum alternating subarray sum of any subarray of _nums.
Input: nums =[3,-1,1,2]Output: 5Explanation:
The subarray [3,-1,1] has the largest alternating subarray sum.The alternating subarray sum is3-(-1)+1=5.
Example 2:
1
2
3
4
5
6
7
Input: nums =[2,2,2,2,2]Output: 2Explanation:
The subarrays [2],[2,2,2], and [2,2,2,2,2] have the largest alternating subarray sum.The alternating subarray sum of [2]is2.The alternating subarray sum of [2,2,2]is2-2+2=2.The alternating subarray sum of [2,2,2,2,2]is2-2+2-2+2=2.
Example 3:
1
2
3
4
5
Input: nums =[1]Output: 1Explanation:
There is only one non-empty subarray, which is[1].The alternating subarray sum is1.
The alternating subarray sum can be seen as a subarray where we alternate between adding and subtracting elements. We can use a dynamic programming approach similar to Kadane’s algorithm, but we need to track two states: the maximum sum ending at the current index with a plus or minus sign.
classSolution {
public:longlong maximumAlternatingSubarraySum(vector<int>& nums) {
longlong even =0, odd = LLONG_MIN, ans = LLONG_MIN;
for (int x : nums) {
longlong new_even = max((longlong)x, odd + x);
longlong new_odd = even - x;
even = new_even;
odd = new_odd;
ans = max(ans, even);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
funcmaximumAlternatingSubarraySum(nums []int) int64 {
even, odd:= int64(0), int64(-1<<63)
ans:= int64(-1<<63)
for_, x:=rangenums {
newEven:= max(int64(x), odd+int64(x))
newOdd:=even- int64(x)
even, odd = newEven, newOddifeven > ans {
ans = even }
}
returnans}
func max(a, bint64) int64 {
ifa > b {
returna }
returnb}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publiclongmaximumAlternatingSubarraySum(int[] nums) {
long even = 0, odd = Long.MIN_VALUE, ans = Long.MIN_VALUE;
for (int x : nums) {
long newEven = Math.max(x, odd + x);
long newOdd = even - x;
even = newEven;
odd = newOdd;
ans = Math.max(ans, even);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funmaximumAlternatingSubarraySum(nums: IntArray): Long {
var even = 0Lvar odd = Long.MIN_VALUE
var ans = Long.MIN_VALUE
for (x in nums) {
val newEven = maxOf(x.toLong(), odd + x)
val newOdd = even - x
even = newEven
odd = newOdd
ans = maxOf(ans, even)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defmaximumAlternatingSubarraySum(self, nums: list[int]) -> int:
even: int =0 odd: int = float('-inf')
ans: int = float('-inf')
for x in nums:
new_even = max(x, odd + x)
new_odd = even - x
even, odd = new_even, new_odd
ans = max(ans, even)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnmaximum_alternating_subarray_sum(nums: Vec<i32>) -> i64 {
letmut even =0i64;
letmut odd =i64::MIN;
letmut ans =i64::MIN;
for&x in&nums {
let new_even = (x asi64).max(odd + x asi64);
let new_odd = even - x asi64;
even = new_even;
odd = new_odd;
ans = ans.max(even);
}
ans
}
}