Skip to content

Commit 8a20275

Browse files
authored
Create 1696-jump-game-vi.js
1 parent e298a50 commit 8a20275

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1696-jump-game-vi.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const maxResult = function(nums, k) {
7+
const n = nums.length;
8+
const f = Array(n).fill(0);
9+
f[0] = nums[0];
10+
const q = [0];
11+
for (let i = 1; i < n; ++i) {
12+
while (i - q[0] > k) {
13+
q.shift();
14+
}
15+
f[i] = f[q[0]] + nums[i];
16+
while (q.length && f[i] >= f[q[q.length - 1]]) {
17+
q.pop();
18+
}
19+
q.push(i);
20+
}
21+
return f[n - 1];
22+
};
23+

0 commit comments

Comments
 (0)