go: add «45. Jump Game II»
URL: https://leetcode.com/problems/jump-game-ii/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
601d5a1f68
commit
ad8b800a0b
1 changed files with 15 additions and 0 deletions
15
go/jump-game-ii.go
Normal file
15
go/jump-game-ii.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package main
|
||||
|
||||
func jump(nums []int) int {
|
||||
jumps, end, furthest := 0, 0, 0
|
||||
|
||||
for i := 0; i < len(nums)-1; i++ {
|
||||
furthest = max(furthest, i+nums[i])
|
||||
if i == end {
|
||||
jumps++
|
||||
end = furthest
|
||||
}
|
||||
}
|
||||
|
||||
return jumps
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue