|
| 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 | + */ |
| 50 | +pub 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 | + |
| 57 | +impl Solution { |
| 58 | + // Credit: https://leetcode.com/problems/maximum-alternating-subsequence-sum/solutions/3416719/rust-solution/ |
| 59 | + pub fn max_alternating_sum(nums: Vec<i32>) -> i64 { |
| 60 | + let n = nums.len(); |
| 61 | + let nums = nums.into_iter().map(|v| v as i64).collect::<Vec<i64>>(); |
| 62 | + let mut a = nums[0]; |
| 63 | + let mut b = 0; |
| 64 | + |
| 65 | + for i in 1..n { |
| 66 | + let v = nums[i]; |
| 67 | + b = b.max(a - v); |
| 68 | + a = a.max(b + v); |
| 69 | + } |
| 70 | + |
| 71 | + a.max(b) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// submission codes end |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use super::*; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test_1911_example_1() { |
| 83 | + let nums = vec![4, 2, 5, 3]; |
| 84 | + |
| 85 | + let result = 7; |
| 86 | + |
| 87 | + assert_eq!(Solution::max_alternating_sum(nums), result); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_1911_example_2() { |
| 92 | + let nums = vec![5, 6, 7, 8]; |
| 93 | + |
| 94 | + let result = 8; |
| 95 | + |
| 96 | + assert_eq!(Solution::max_alternating_sum(nums), result); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_1911_example_3() { |
| 101 | + let nums = vec![6, 2, 1, 2, 4, 5]; |
| 102 | + |
| 103 | + let result = 10; |
| 104 | + |
| 105 | + assert_eq!(Solution::max_alternating_sum(nums), result); |
| 106 | + } |
| 107 | +} |
0 commit comments