Skip to content

Latest commit

 

History

History

1911

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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.

  • For example, the alternating sum of [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

Companies:
Quince

Related Topics:
Dynamic Programming

Solution 1. DP

Intuition: The brute force way is enumerating all the 2^N subsequences which has lots of repetitive computation. Given the first i + 1 elements A[0...i], the greatest alternating sum is a fixed value, we can memoize it and keep extending i. So we should use Dynamic Programming.

Algorithm:

Let dp[i+1][0] and dp[i+1][1] be the maximum alternating subsequence sum of the first i + 1 elements A[0...i] where A[i] is even-indexed and odd-indexed, respectively.

dp[i+1][0] = max(
                    dp[i][1] + A[i],  // if we pick A[i] as the last even-indexed number
                    dp[i][0]          // otherwise
                )
dp[i+1][1] = max(
                    dp[i][0] - A[i],  // if we pick A[i] as the last odd-indexed number
                    dp[i][1]          // otherwise
                )

dp[0][0] = dp[0][1] = 0

The answer must has odd number of elements, so must be dp[N][0].

Since dp[i+1][?] is only dependent on dp[i][?], instead of using an N x 2 array, we can simply using a 1 x 2 array to store the DP values.

// OJ: https://leetcode.com/contest/biweekly-contest-55/problems/maximum-alternating-subsequence-sum/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
    typedef long long LL;
public:
    long long maxAlternatingSum(vector<int>& A) {
        LL N = A.size(), dp[2] = {};
        for (int i = 0; i < N; ++i) {
            LL next[2] = {};
            next[0] = max(dp[1] + A[i], dp[0]);
            next[1] = max(dp[0] - A[i], dp[1]);
            swap(next, dp);
        }
        return dp[0];
    }
};

Or

// OJ: https://leetcode.com/problems/maximum-alternating-subsequence-sum/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    long long maxAlternatingSum(vector<int>& A) {
        long long even = 0, odd = 0;
        for (int n : A) {
            long long o = even - n, e = odd + n;
            even = max(even, e);
            odd = max(odd, o);
        }
        return even; 
    }
};