Files
LeetCode-Go/leetcode/0045.Jump-Game-II/45. Jump Game II.go
2021-03-11 21:51:36 +08:00

22 lines
329 B
Go

package leetcode
func jump(nums []int) int {
if len(nums) == 1 {
return 0
}
needChoose, canReach, step := 0, 0, 0
for i, x := range nums {
if i+x > canReach {
canReach = i + x
if canReach >= len(nums)-1 {
return step + 1
}
}
if i == needChoose {
needChoose = canReach
step++
}
}
return step
}