1696. 跳跃游戏 VI

1696. 跳跃游戏 VI

解法一: 单调栈

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func maxResult(nums []int, k int) int {
n := len(nums)
dp := make([]int, n)
dp[0] = nums[0]
stack := []int{0}
for i := 1; i < n; i++ {
if stack[0] < i-k {
stack = stack[1:]
}
dp[i] = nums[i] + dp[stack[0]]
for len(stack) > 0 && dp[stack[len(stack)-1]] <= dp[i] {
stack = stack[:len(stack)-1]
}
stack = append(stack, i)
}
return dp[n-1]
}
作者

wuhunyu

发布于

2024-02-05

更新于

2024-02-05

许可协议