blankyao's blog

ARTS 016

July 01, 2020

Algorithm

55. Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

Solution

fun canJump(nums: IntArray): Boolean {
    var max = 0
    for (index in nums.indices) {
        if (max < index) return false
        max = max.coerceAtLeast(index + nums[index])
        if (max >= nums.size - 1) return true
    }
    return false
}

Review

Figma’s engineering values

Tips

None

Share

None


Written by blankyao who lives and works in Amoy building useful things.