[Leetcode] [Python]55:jump Game

Source: Internet
Author: User

#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '

55:jump Game
https://leetcode.com/problems/jump-game/

Given an array of non-negative integers, you is initially positioned at the first index of the array.
Each element of the array represents your maximum jump length is at that position.
Determine if you is able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.

= = = Comments by dabay===
Each calculation can reach a new range.
If the new range goes beyond Len (a)-1, the range is true.
‘‘‘

Class Solution:
# @param A, a list of integers
# @return A Boolean
def canjump (self, A):
If Len (A) <= 1:
Return True
Start = 0
End = start + A[start]
While end >= start:
If End >= Len (A)-1:
Return True
New_end = End
For I in xrange (Start, end+1):
New_end = max (new_end, i + a[i])
Start, end = End+1, new_end
Else
Return False


def main ():
Sol = solution ()
A = [1,1,1,0]
Print Sol.canjump (A)


if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)

[Leetcode] [Python]55: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.