Jump Game

Problem statement

Start at index 0. From index i you may jump forward by at most nums[i] steps. Decide whether the last index is reachable.

Example:

Input:

[2, 3, 1, 1, 4]

Expected output:

true

Why: one path is 0 → 1 → 4.

Practice on LeetCode: 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
}