Dynamic Programming Algorithm With Space Compression Optimization to solve "1911. Maximum Alternating Subsequence Sum"

2023-07-11 16:40:08
Using dynamic programming algorithm with space compression optimization to solve the "1911. Maximum Alternating Subsequence Sum" problem.

Example

1911. Maximum Alternating Subsequence Sum

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.

Answers

Dynamic Programming with space compression optimization

var maxAlternatingSum = function(nums) {
  let even = nums[0], odd = 0
  for (const num of nums) {
    const t = even
    even = Math.max(even, odd + num)
    odd = Math.max(odd, t - num)
  }
  return Math.max(even, odd)
};
func maxAlternatingSum(nums []int) int64 {
  even, odd := int64(nums[0]), int64(0)
  for _, num := range nums {
    t := even
    even = max(even, odd + int64(num))
    odd = max(odd, t - int64(num))
  }
  return max(even, odd)
}
func max(a, b int64) int64 {
  if a > b {
    return a
  }
  return b
}
class Solution {
  function maxAlternatingSum($nums) {
    $even = $nums[0];
    $odd = 0;
    foreach ($nums as $num) {
      $t = $even;
      $even = max($even, $odd + $num);
      $odd = max($odd, $even - $num);
    }
    return max($even, $odd);
  }
}
class Solution {
  public long maxAlternatingSum(int[] nums) {
    long even = nums[0], odd = 0;
    for (int num : nums) {
      long t = even;
      even = Math.max(even, odd + num);
      odd = Math.max(odd, t - num);
    }
    return Math.max(even, odd);
  }
}
public class Solution {
  public long MaxAlternatingSum(int[] nums) {
    long even = nums[0], odd = 0;
    foreach (int num in nums) {
      long t = even;
      even = Math.Max(even, odd + num);
      odd = Math.Max(odd, t - num);
    }
    return Math.Max(even, odd);
  }
}
class Solution {
public:
  long long maxAlternatingSum(vector<int>& nums) {
    long long even = nums[0], odd = 0;
    for (int num : nums) {
      long long t = even;
      even = max(even, odd + num);
      odd = max(odd, even - num);
    }
    return max(even, odd);
  }
};
#define MAX(a, b) (a > b ? a : b)
long long maxAlternatingSum(int* nums, int numsSize){
  long long even = nums[0], odd = 0;
  for (int i = 0; i < numsSize; i++) {
    long long t = even;
    even = MAX(even, odd + nums[i]);
    odd = MAX(odd, even - nums[i]);
  }
  return MAX(even, odd);
}
class Solution:
  def maxAlternatingSum(self, nums: List[int]) -> int:
    even, odd = nums[0], 0
    for num in nums: 
      t = even
      even, odd = max(even, odd + num), max(odd, t - num)
    return max(even, odd)

Dynamic Programming Algorithm with Space Compression Optimization and different ways to shallow copy a array list
Using dynamic programming algorithm with space compression optimization and different ways to shallow copy a Array List, solving the "931. Minimum Falling Path Sum"
动态规划 + 状态压缩 + bitCount 统计二进制 1 个数,求解《1681. 最小不兼容性》
Go / Java / C# / C / C++ / Python / PHP 内置 bitCount,动态规划 + 状态压缩,求解《1681. 最小不兼容性》
动态规划,求解《940. 不同的子序列 II》
动态规划,求解《940. 不同的子序列 II》