1911. Maximum Alternating Subsequence Sum Medium

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [1911] Maximum Alternating Subsequence Sum
3 *
4 * 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.
5 * 
6 * 
7 * 	For example, the alternating sum of [4,2,5,3] is (4 + 5) - (2 + 3) = 4.
8 * 
9 * 
10 * Given an array nums, return the maximum alternating sum of any subsequence of nums (after reindexing the elements of the subsequence).
11 * 
12 * 
13 * 
14 * 
15 * 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,<u>2</u>,3,<u>7</u>,2,1,<u>4</u>] (the underlined elements), while [2,4,2] is not.
16 * 
17 *  
18 * Example 1:
19 * 
20 * 
21 * Input: nums = [<u>4</u>,<u>2</u>,<u>5</u>,3]
22 * Output: 7
23 * Explanation: It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
24 * 
25 * 
26 * Example 2:
27 * 
28 * 
29 * Input: nums = [5,6,7,<u>8</u>]
30 * Output: 8
31 * Explanation: It is optimal to choose the subsequence [8] with alternating sum 8.
32 * 
33 * 
34 * Example 3:
35 * 
36 * 
37 * Input: nums = [<u>6</u>,2,<u>1</u>,2,4,<u>5</u>]
38 * Output: 10
39 * Explanation: It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.
40 * 
41 * 
42 *  
43 * Constraints:
44 * 
45 * 
46 * 	1 <= nums.length <= 10^5
47 * 	1 <= nums[i] <= 10^5
48 * 
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/maximum-alternating-subsequence-sum/
53// discuss: https://leetcode.com/problems/maximum-alternating-subsequence-sum/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58    pub fn max_alternating_sum(nums: Vec<i32>) -> i64 {
59        
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_1911() {
71    }
72}
73


Back
© 2025 bowen.ge All Rights Reserved.