-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path1696-jump-game-vi.js
46 lines (39 loc) · 1.16 KB
/
1696-jump-game-vi.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
43
44
45
46
/**
* 1696. Jump Game VI
* https://leetcode.com/problems/jump-game-vi/
* Difficulty: Medium
*
* You are given a 0-indexed integer array nums and an integer k.
*
* You are initially standing at index 0. In one move, you can jump at most k steps forward without
* going outside the boundaries of the array. That is, you can jump from index i to any index in
* the range [i + 1, min(n - 1, i + k)] inclusive.
*
* You want to reach the last index of the array (index n - 1). Your score is the sum of all
* nums[j] for each index j you visited in the array.
*
* Return the maximum score you can get.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maxResult = function(nums, k) {
const n = nums.length;
const maxScores = new Array(n).fill(-Infinity);
const deque = [];
maxScores[0] = nums[0];
deque.push(0);
for (let i = 1; i < n; i++) {
while (deque.length && deque[0] < i - k) {
deque.shift();
}
maxScores[i] = nums[i] + maxScores[deque[0]];
while (deque.length && maxScores[i] >= maxScores[deque[deque.length - 1]]) {
deque.pop();
}
deque.push(i);
}
return maxScores[n - 1];
};