Leetcode Problem Solving Jump Game original problem
Each value in the array indicates that the current position can jump a few steps forward, judging whether the given array has a jump to the end.
Note the point:
- All numbers are positive.
- The number of steps to jump can be smaller than the current value
Example:
Input: Nums = [2, 3, 1, 1, 4]
Output: True
Input: Nums = [3, 2, 1, 0, 4]
Output: False
Thinking of solving problems
First think about when you can not complete the jump, before the current position (including the current position) can jump to the farthest distance is the current position, and this time has not reached the end, what kind of situation can be guaranteed to jump to the end, as long as the current maximum distance beyond the end. As long as the current position does not exceed the maximum distance you can jump to, you can constantly refresh the farthest distance to continue to move forward.
AC Source
class solution(object): def canjump(self, nums): "" : Type Nums:list[int]: Rtype:bool " " " if notNumsreturn Falselength = Len (nums) index =0Longest = nums[0] whileIndex <= Longest:ifLongest >= Length-1:return TrueLongest = max (longest, index + Nums[index]) Index + =1 return Falseif__name__ = ="__main__":assertSolution (). Canjump ([2,3,1,1,4]) ==True assertSolution (). Canjump ([3,2,1,0,4]) ==False
Welcome to my GitHub to get the relevant source code.
Leetcode Jump Game