-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjump_game.py
More file actions
30 lines (23 loc) · 795 Bytes
/
Copy pathjump_game.py
File metadata and controls
30 lines (23 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
# @param A, a list of integers
# @return a boolean
def canJump(self, A):
n = len(A)
if n == 1:
return True
furthest_position = A[0]
if furthest_position >= n - 1:
return True
if furthest_position == 0:
return False
for i in range(1, n):
if furthest_position >= i and A[i] + i >= n - 1:
return True
if furthest_position <= i and A[i] == 0:
return False
furthest_position = max(furthest_position, A[i] + i)
return False
if __name__ == '__main__':
print Solution().canJump([2, 3, 1, 1, 4])
print Solution().canJump([3, 2, 1, 0, 4])
print Solution().canJump(range(25000, 0, -1) + [1, 0, 0])