045. Jump Game II
Question 45
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.Answer
class Solution:
def jump(self, nums: 'List[int]') -> 'int':
lennums = len(nums)
step, end, start, maxend = 0, 0, 0, 0
while end < lennums - 1 :
step += 1
maxend = end + 1
for i in range(start, end+1):
maxend = max( maxend, i + nums[i] )
end, start = maxend, end + 1
return stepLast updated