Leetcode Jump Game

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.