-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJumpGame.py
53 lines (51 loc) · 1.13 KB
/
JumpGame.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
2 1 0 2 0 1 1 1
"""
n = len(nums)
if n == 0:
return False
if n == 1:
return True
if n == 2:
return nums[0] > 0
curr = n - 2
next = n - 1
while curr >= 0:
if nums[curr] < next - curr:
curr -= 1
else:
next = curr
curr -= 1
if curr < 0:
return True
return False
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
2 1 3 2 0 1 1 1
"""
n = len(nums)
i = 0
reach = 0
while i < n and i <= reach:
reach = max(reach, i + nums[i])
i += 1
return i == n
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
n = len(nums)
last = n - 1
for i in range(n-2, -1, -1):
if i + nums[i] >= last:
last = i
return last == 0