|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../diagonal-traverse-ii "Diagonal Traverse II") |
| 9 | + |
| 10 | +[Next >](../counting-elements "Counting Elements") |
| 11 | + |
| 12 | +## [1425. Constrained Subsequence Sum (Hard)](https://leetcode.com/problems/constrained-subsequence-sum "带限制的子序列和") |
| 13 | + |
| 14 | +<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> |
| 15 | + |
| 16 | +<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> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong>Example 1:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> nums = [10,2,-10,5,20], k = 2 |
| 23 | +<strong>Output:</strong> 37 |
| 24 | +<b>Explanation:</b> The subsequence is [10, 2, 5, 20]. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong>Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> nums = [-1,-2,-3], k = 1 |
| 31 | +<strong>Output:</strong> -1 |
| 32 | +<b>Explanation:</b> The subsequence must be non-empty, so we choose the largest number. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong>Example 3:</strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input:</strong> nums = [10,-2,-10,-5,20], k = 2 |
| 39 | +<strong>Output:</strong> 23 |
| 40 | +<b>Explanation:</b> The subsequence is [10, -2, -5, 20]. |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= k <= nums.length <= 10^5</code></li> |
| 48 | + <li><code>-10^4 <= nums[i] <= 10^4</code></li> |
| 49 | +</ul> |
| 50 | + |
| 51 | +### Related Topics |
| 52 | + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] |
| 53 | + |
| 54 | +### Hints |
| 55 | +<details> |
| 56 | +<summary>Hint 1</summary> |
| 57 | +Use dynamic programming. |
| 58 | +</details> |
| 59 | + |
| 60 | +<details> |
| 61 | +<summary>Hint 2</summary> |
| 62 | +Let dp[i] be the solution for the prefix of the array that ends at index i, if the element at index i is in the subsequence. |
| 63 | +</details> |
| 64 | + |
| 65 | +<details> |
| 66 | +<summary>Hint 3</summary> |
| 67 | +dp[i] = nums[i] + max(0, dp[i-k], dp[i-k+1], ..., dp[i-1]) |
| 68 | +</details> |
| 69 | + |
| 70 | +<details> |
| 71 | +<summary>Hint 4</summary> |
| 72 | +Use a heap with the sliding window technique to optimize the dp. |
| 73 | +</details> |
0 commit comments