Jump Game - Golang
Problem statement:
You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
Example:
Input:
[2,3,1,1,4]
Expected output:
true
If you would like to solve the problem on Leetcode, here is the link to the problem: https://leetcode.com/problems/jump-game
Golang Solution:
func canJump(nums []int) bool {
i := 0
for j := len(nums)-1; j>=0; j-- {
if j + nums[j] >= i {
i = j
}
}
return i == 0
}