-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1425-constrained-subsequence-sum.js
42 lines (36 loc) · 1.13 KB
/
1425-constrained-subsequence-sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 1425. Constrained Subsequence Sum
* https://leetcode.com/problems/constrained-subsequence-sum/
* Difficulty: Hard
*
* Given an integer array nums and an integer k, return the maximum sum of a non-empty subsequence
* of that array such that for every two consecutive integers in the subsequence, nums[i] and
* nums[j], where i < j, the condition j - i <= k is satisfied.
*
* A subsequence 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.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var constrainedSubsetSum = function(nums, k) {
const maxSums = [...nums];
const deque = [];
let result = nums[0];
for (let i = 0; i < nums.length; i++) {
while (deque.length && deque[0] < i - k) {
deque.shift();
}
if (deque.length) {
maxSums[i] = Math.max(maxSums[i], maxSums[deque[0]] + nums[i]);
}
while (deque.length && maxSums[deque[deque.length - 1]] <= maxSums[i]) {
deque.pop();
}
deque.push(i);
result = Math.max(result, maxSums[i]);
}
return result;
};