mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
37 lines
1.3 KiB
HTML
37 lines
1.3 KiB
HTML
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i < j</code>, the condition <code>j - i <= k</code> is satisfied.</p>
|
|
|
|
<p>A <em>subsequence</em> of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [10,2,-10,5,20], k = 2
|
|
<strong>Output:</strong> 37
|
|
<b>Explanation:</b> The subsequence is [10, 2, 5, 20].
|
|
</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [-1,-2,-3], k = 1
|
|
<strong>Output:</strong> -1
|
|
<b>Explanation:</b> The subsequence must be non-empty, so we choose the largest number.
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [10,-2,-10,-5,20], k = 2
|
|
<strong>Output:</strong> 23
|
|
<b>Explanation:</b> The subsequence is [10, -2, -5, 20].
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
|
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
|
</ul>
|