Class Solution
java.lang.Object
g1901_2000.s1911_maximum_alternating_subsequence_sum.Solution
1911 - Maximum Alternating Subsequence Sum.<p>Medium</p>
<p>The <strong>alternating sum</strong> of a <strong>0-indexed</strong> array is defined as the <strong>sum</strong> of the elements at <strong>even</strong> indices <strong>minus</strong> the <strong>sum</strong> of the elements at <strong>odd</strong> indices.</p>
<ul>
<li>For example, the alternating sum of <code>[4,2,5,3]</code> is <code>(4 + 5) - (2 + 3) = 4</code>.</li>
</ul>
<p>Given an array <code>nums</code>, return <em>the <strong>maximum alternating sum</strong> of any subsequence of</em> <code>nums</code> <em>(after <strong>reindexing</strong> the elements of the subsequence)</em>.</p>
<p>A <strong>subsequence</strong> 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, <code>[2,7,4]</code> is a subsequence of <code>[4,2,3,7,2,1,4]</code> (the underlined elements), while <code>[2,4,2]</code> is not.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [4,2,5,3]</p>
<p><strong>Output:</strong> 7</p>
<p><strong>Explanation:</strong> It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [5,6,7,8]</p>
<p><strong>Output:</strong> 8</p>
<p><strong>Explanation:</strong> It is optimal to choose the subsequence [8] with alternating sum 8.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [6,2,1,2,4,5]</p>
<p><strong>Output:</strong> 10</p>
<p><strong>Explanation:</strong> It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maxAlternatingSum
public long maxAlternatingSum(int[] nums)
-