Jump Game II
Example
Solution
class Solution:
# @param A, a list of integers
# @return an integer
# We use "last" to keep track of the maximum distance that has been reached
# by using the minimum steps "ret", whereas "curr" is the maximum distance
# that can be reached by using "ret+1" steps. Thus,curr = max(i+A[i]) where 0 <= i <= last.
def jump(self, A):
ret = 0
last = 0 #last is the steps that has been reached.
curr = 0 #max distance
for i in range(len(A)):
if i > last:
last = curr
ret += 1
curr = max(curr, i+A[i])
return retLast updated