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.
Example
Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Solution
# key is to find out a right algorithm to calculate the best local result
class Solution(object):
def canJump(self, nums):
m = 0
i = 0
while (m < len(nums)-1):
if i > m:
return False
m = max(m, i+nums[i])
i += 1
return True