-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjump-game.py
71 lines (64 loc) · 1.97 KB
/
jump-game.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Given an array of non-negative integers, you are initially positioned at the first index of the array.
#
# Each element in the array represents your maximum jump length at that position.
#
# Determine if you are able to reach the last index.
#
# Example 1:
#
# Input: [2,3,1,1,4]
# Output: true
# Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
# Example 2:
#
# Input: [3,2,1,0,4]
# Output: false
# Explanation: You will always arrive at index 3 no matter what. Its maximum
# jump length is 0, which makes it impossible to reach the last index.
from typing import List
class Solution0:
def canJump(self, nums: List[int]) -> bool:
# reachable_set = {len(nums) - 1}
final = len(nums) - 1
for index in range(len(nums) - 2, -1, -1):
jump = nums[index]
if index + jump >= final:
final = index
return final == 0
class Solution:
def canJump(self, nums: List[int]) -> bool:
return self._canJump(nums=nums, index=0, best_final=-1) >= len(nums) - 1
def _canJump(self, nums, index, best_final):
best_final = max(best_final, index + nums[index])
length = len(nums)
for jump in range(nums[index], 0, -1):
next_index = index + jump
if next_index >= length:
return next_index
if next_index + nums[next_index] <= best_final:
continue
best_final = max(best_final, self._canJump(nums, next_index, best_final))
if best_final >= length:
return best_final
return best_final
if __name__ == '__main__':
cases = [
(
([2, 3, 1, 1, 4],),
True,
),
(
([3, 2, 1, 0, 4],),
False
),
(
([2, 0],),
True
),
(
([0],),
True
),
]
from test_utils import run_test_cases
run_test_cases(Solution, cases)