Overview
There is an input array provided. Each entry in the array represents the maximum jump length from that position. One is supposed to start from the first index and return true if the last index can be reached and return false if the last index cannot be reached
Example
Input: [2, 3, 1, 1, 4]
Output: true
Input: [3, 2, 1, 0, 4]
Output: false
In the first example, there are different ways to reach the last index.
- 0-1-4
- 0-2-3-4
In the second example, there is no way to reach the last index. The best you can reach is the second last index. Since the value at the second last index is zero, therefore it is not possible to reach the last index.
There is another variation where you have to return the minimum number of jumps. We will look at the program later
Program
package main
import "fmt"
func canJump(nums []int) bool {
lenNums := len(nums)
canJumpB := make([]bool, lenNums)
canJumpB[0] = true
for i := 0; i < lenNums; i++ {
if canJumpB[i] {
valAtCurrIndex := nums[i]
for k := 1; k <= valAtCurrIndex && i+k < lenNums; k++ {
canJumpB[i+k] = true
}
}
}
return canJumpB[lenNums-1]
}
func main() {
input := []int{2, 3, 1, 1, 4}
canJumpOrNot := canJump(input)
fmt.Println(canJumpOrNot)
input = []int{3, 2, 1, 0, 4}
canJumpOrNot = canJump(input)
fmt.Println(canJumpOrNot)
}
Output
true
false
There is another variation where you have to return the minimum number of jumps. Here is the program for the same
package main
import "fmt"
func jump(nums []int) int {
lenJump := len(nums)
minJumps := make([]int, lenJump)
for i := 0; i < lenJump; i++ {
minJumps[i] = -1
}
minJumps[0] = 0
for i := 0; i < lenJump; i++ {
currVal := nums[i]
for j := 1; j <= currVal && i+j < lenJump; j++ {
if minJumps[i+j] == -1 || minJumps[i+j] > minJumps[i]+1 {
minJumps[i+j] = minJumps[i] + 1
}
}
}
return minJumps[lenJump-1]
}
func main() {
input := []int{2, 3, 1, 1, 4}
minJump := jump(input)
fmt.Println(minJump)
input = []int{3, 2, 1, 0, 4}
minJump = jump(input)
fmt.Println(minJump)
}
Output
2
-1