-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1696_jump_game_vi.go
More file actions
39 lines (32 loc) · 964 Bytes
/
s1696_jump_game_vi.go
File metadata and controls
39 lines (32 loc) · 964 Bytes
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
/*
https://leetcode.com/problems/jump-game-vi/
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.
*/
package solutions
func maxResult(nums []int, k int) int {
n := len(nums)
score := make([]int, n)
score[0] = nums[0]
dq := Deque[int]{}
dq.PushLast(0)
for i := 1; i < n; i++ {
for !dq.IsEmpty() && dq.PeekFirst() < i-k {
dq.PopFirst()
}
score[i] = score[dq.PeekFirst()] + nums[i]
for !dq.IsEmpty() && score[i] >= score[dq.PeekLast()] {
dq.PopLast()
}
dq.PushLast(i)
}
return score[n-1]
}