|
| 1 | +<h2><a href="https://leetcode.com/problems/constrained-subsequence-sum">Constrained Subsequence Sum</a></h2> <img src='https://img.shields.io/badge/Difficulty-Hard-red' alt='Difficulty: Hard' /><hr><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> |
| 2 | + |
| 3 | +<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> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong class="example">Example 1:</strong></p> |
| 7 | + |
| 8 | +<pre> |
| 9 | +<strong>Input:</strong> nums = [10,2,-10,5,20], k = 2 |
| 10 | +<strong>Output:</strong> 37 |
| 11 | +<b>Explanation:</b> The subsequence is [10, 2, 5, 20]. |
| 12 | +</pre> |
| 13 | + |
| 14 | +<p><strong class="example">Example 2:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> nums = [-1,-2,-3], k = 1 |
| 18 | +<strong>Output:</strong> -1 |
| 19 | +<b>Explanation:</b> The subsequence must be non-empty, so we choose the largest number. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong class="example">Example 3:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> nums = [10,-2,-10,-5,20], k = 2 |
| 26 | +<strong>Output:</strong> 23 |
| 27 | +<b>Explanation:</b> The subsequence is [10, -2, -5, 20]. |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li> |
| 35 | + <li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li> |
| 36 | +</ul> |
0 commit comments