Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1911.html

1911. Maximum Alternating Subsequence Sum

Level

Medium

Description

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], 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 <= 10^5
  • 1 <= nums[i] <= 10^5

Solution

Use dynamic programming. For 0 <= i < nums.length, let dp[i][0] represent the maximum alternating subsequence sum at index i when nums[i] is used as an even index or not used, and dp[i][1] represent the maximum alternating subsequence sum at index i when nums[i] is used as an odd index or not used.

Initially, dp[0][0] = nums[0] and dp[0][1] = 0 (since all elements in nums are nonnegative). For 1 <= i < nums.length, there exist relations dp[i][0] = Math.max(Math.max(dp[i - 1][1], 0) + nums[i], dp[i - 1][0]) and dp[i][1] = Math.max(Math.max(dp[i - 1][0] - nums[i], 0), dp[i - 1][1]).

Finally, return the maximum of dp[nums.length - 1][0] and dp[nums.length - 1][1].

  • class Solution {
        public long maxAlternatingSum(int[] nums) {
            int length = nums.length;
            long[][] dp = new long[length][2];
            dp[0][0] = nums[0];
            for (int i = 1; i < length; i++) {
                dp[i][0] = Math.max(Math.max(dp[i - 1][1], 0) + nums[i], dp[i - 1][0]);
                dp[i][1] = Math.max(Math.max(dp[i - 1][0] - nums[i], 0), dp[i - 1][1]);
            }
            return Math.max(dp[length - 1][0], dp[length - 1][1]);
        }
    }
    
    ############
    
    class Solution {
        public long maxAlternatingSum(int[] nums) {
            long f = 0, g = 0;
            for (int x : nums) {
                long ff = Math.max(g - x, f);
                long gg = Math.max(f + x, g);
                f = ff;
                g = gg;
            }
            return Math.max(f, g);
        }
    }
    
  • // OJ: https://leetcode.com/contest/biweekly-contest-55/problems/maximum-alternating-subsequence-sum/
    // 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];
        }
    };
    
  • # 1911. Maximum Alternating Subsequence Sum
    # https://leetcode.com/problems/maximum-alternating-subsequence-sum
    
    class Solution:
        def maxAlternatingSum(self, nums: List[int]) -> int:
            n = len(nums)
            
            odd = [0] * n
            even = [0] * n
            
            odd[0] = nums[0]
            
            for i in range(1, n):
                odd[i] = max(odd[i - 1], even[i - 1] + nums[i])
                even[i] = max(even[i - 1], odd[i - 1] - nums[i])
    
            return odd[-1]
    
    
  • func maxAlternatingSum(nums []int) int64 {
    	var f, g int
    	for _, x := range nums {
    		f, g = max(g-x, f), max(f+x, g)
    	}
    	return int64(max(f, g))
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    
  • function maxAlternatingSum(nums: number[]): number {
        let [f, g] = [0, 0];
        for (const x of nums) {
            [f, g] = [Math.max(g - x, f), Math.max(f + x, g)];
        }
        return g;
    }
    
    

All Problems

All Solutions