The alternating sum of a 0-indexed array is defined as the sum of the elements at even indices minus the sum of the elements at odd indices.
[4,2,5,3]
is (4 + 5) - (2 + 3) = 4
.Given an array nums
, return the maximum alternating sum of any subsequence of nums
(after reindexing the elements of the subsequence).
A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. For example, [2,7,4]
is a subsequence of [4,2,3,7,2,1,4]
(the underlined elements), while [2,4,2]
is not.
Example 1:
Input: nums = [4,2,5,3] Output: 7 Explanation: It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
Example 2:
Input: nums = [5,6,7,8] Output: 8 Explanation: It is optimal to choose the subsequence [8] with alternating sum 8.
Example 3:
Input: nums = [6,2,1,2,4,5] Output: 10 Explanation: It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 105
The problem asks for the maximum alternating sum of any subsequence of a given array. The alternating sum is calculated by summing elements at even indices and subtracting the sum of elements at odd indices. The solution uses dynamic programming to efficiently find this maximum sum.
This approach uses two arrays, f
and g
, to store the maximum alternating sums.
f[i]
represents the maximum alternating sum of a subsequence ending at index i-1
where the last element is considered an even index (i.e., subtracted).
g[i]
represents the maximum alternating sum of a subsequence ending at index i-1
where the last element is considered an odd index (i.e., added).
The recurrence relations are as follows:
f[i] = max(g[i-1] - nums[i-1], f[i-1])
: We either include the current element (subtracting it from the previous odd sum g[i-1]
) or exclude it.
g[i] = max(f[i-1] + nums[i-1], g[i-1])
: We either include the current element (adding it to the previous even sum f[i-1]
) or exclude it.
The base cases are f[0] = g[0] = 0
. The final answer is max(f[n], g[n])
, where n
is the length of the input array.
Time Complexity: O(n), where n is the length of the input array. We iterate through the array once.
Space Complexity: O(n) due to the use of f
and g
arrays.
This approach optimizes the space complexity by using only two variables, f
and g
, instead of arrays. It maintains the same logic as Approach 1 but avoids the extra space.
Time Complexity: O(n), same as Approach 1.
Space Complexity: O(1), as we only use two variables. This is a significant improvement over Approach 1.
The code implementations for both approaches in various languages are provided in the original response. Approach 2 is generally preferred due to its better space complexity. The code snippets are well-commented and easy to follow. They directly reflect the recurrence relations described above.
Remember to choose the appropriate approach based on your needs. If memory usage is a significant concern, Approach 2 is the better choice. Otherwise, Approach 1 might be slightly easier to understand.