55. Jump Game
problem description
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.
Example 1:
Example 2:
algorithm thought
和之前jump game2类似。这里不限制你跳跃的次数,我们只需要每次记录能达到的最远距离即可。每次循环开始的时候更新最大值。如果更新之后的最大值和当前值是一样的话,说明只能跳到这里了,return false。如果所有的跳完,return true
code
algorithm analysis
算法遍历一次数组,时间复杂度O(n)。
Last updated