ARTS 016
July 01, 2020
Algorithm
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
Tips
None
Share
None