Created
January 19, 2022 22:33
-
-
Save CollinShoop/22dc593432c0aa117636021f7678ac4a to your computer and use it in GitHub Desktop.
https://leetcode.com/problems/jump-game/ (~25%) Could be better
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func canJump(nums []int) bool { | |
// can-jump cache | |
cache := map[int]bool{ | |
0: true, | |
} | |
var canJump func(int) bool | |
canJump = func(to int) bool { | |
if canJumpTo, ok := cache[to]; ok { | |
return canJumpTo | |
} | |
for i := 0; i < to; i++ { | |
// check can jump from position "i" to "to" | |
if i + nums[i] >= to { | |
// if so, can you jump to "i"? | |
if ok := canJump(i); ok { | |
cache[to] = true | |
return true | |
} | |
} | |
} | |
cache[to] = false | |
return false | |
} | |
// work backwards from the end | |
return canJump(len(nums)-1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment