1132. Maximum Alternating Subsequence Sum

EasyDynamic Programming1D DP

Given an array nums, the alternating sum of a subsequence is the sum of its elements at even indices minus the sum at odd indices (0-indexed within the subsequence). Return the maximum alternating sum over all subsequences. The input is JSON {nums}.

Input: JSON {nums}.

Output: Integer — the maximum alternating subsequence sum.

Examples

Example 1
Input: {"nums":[4,2,5,3]}
Output: 7
Explanation: Subsequence [4,2,5] gives 4-2+5=7.
Example 2
Input: {"nums":[6,2,1,2,4,5]}
Output: 10
Explanation: An optimal subsequence sums to 10.
Example 3
Input: {"nums":[5]}
Output: 5
Explanation: A single element.

Constraints

Asked by

GoogleMeta
Solve this problem in the editor →