Skip to content

Instantly share code, notes, and snippets.

@sarathsp06
Created March 10, 2019 20:50
Show Gist options
  • Save sarathsp06/9d44b6032b85069a4e350a8f55e10a78 to your computer and use it in GitHub Desktop.
Save sarathsp06/9d44b6032b85069a4e350a8f55e10a78 to your computer and use it in GitHub Desktop.
Steps jump
package main
import (
"fmt"
)
func getMaxValIdx(arr []int, first, last int) int {
maxVal := arr[last]
maxValIdx := last
for idx, val := range arr[first+1 : last+1] {
if val >= maxVal {
maxVal = val
maxValIdx = idx + first + 1
}
}
return maxValIdx
}
func checkPath(arr []int) bool {
for idx, val := range arr {
arr[idx] = idx + val
}
idx := 0
lastIdx := len(arr) - 1
for {
maxIdx := arr[idx]
if maxIdx >= lastIdx {
return true
}
if maxIdx == idx {
return false
}
idx = getMaxValIdx(arr, idx, maxIdx)
}
}
func main() {
fmt.Println(checkPath([]int{1, 3, 1, 2, 0, 1}))
fmt.Println(checkPath([]int{1, 2, 1, 0, 0, 0}))
fmt.Println(checkPath([]int{0}))
}
@sarathsp06
Copy link
Author

screenshot_20190223-042748__01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment