Leetcode Jump Game II

Source: Internet
Author: User

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

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.