Leetcode Problem Solving Jump Game II original question
Each value in the array indicates that the current position can jump a few steps forward, judging at least a few steps to jump to the end.
Note the point:
- All numbers are positive.
- The number of steps to jump can be smaller than the current value
- Ensure that all test cases are able to jump to the last
Example:
Input: Nums = [2, 3, 1, 1, 4]
Output: 2
Thinking of solving problems
This is the question given on the jump Game, and the title is guaranteed to last. Iterate through the array, starting at the current coordinates all jumps can reach the farthest distance is reach, we jump N step can reach the furthest distance with longest, if longest can not reach the current coordinates, it is necessary to jump a step, jump directly to the current coordinates before the point can jump to the farthest position.
AC Source
class solution(object): def Jump(self, nums): "" : Type Nums:list[int]: Rtype:int " " "length = Len (nums) counter =0Longest =0reach =0 forIinchRange (length):ifLongest < I:counter + =1Longest = reach reach = Max (reach, nums[i] + i)returnCounterif__name__ = ="__main__":assertSolution (). Jump ([2,3,1,1,4]) ==2
Welcome to my GitHub to get the relevant source code.
Leetcode Jump Game II