Created
March 10, 2019 20:50
-
-
Save sarathsp06/9d44b6032b85069a4e350a8f55e10a78 to your computer and use it in GitHub Desktop.
Steps jump
This file contains hidden or 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
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})) | |
} |
Author
sarathsp06
commented
Mar 10, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment