#-*-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