Skip to content

Commit 44b815b

Browse files
committed
跳跃游戏
1 parent 8a07f61 commit 44b815b

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

greedy/jump.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
def canJump(nums):
2-
n = len(nums)
3-
max_reach = 0
1+
from typing import List
42

5-
for i in range(n):
6-
if i > max_reach:
7-
return False
8-
max_reach = max(max_reach, i + nums[i])
3+
class Solution:
4+
def canJump(self, nums: List[int]) -> bool:
5+
n = len(nums)
6+
max_reach = 0
97

10-
return True
8+
for i in range(n):
9+
## 能不能到达重点的本质是 max_reach 和 i 的比较。
10+
if i > max_reach:
11+
return False
12+
max_reach = max(max_reach, i + nums[i])
13+
14+
return True
15+
16+
17+
nums = [3,2,1,0,1]
18+
print(Solution().canJump(nums))

greedy/test_jump.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def canJump(self , nums):
3+
max_reach = 0
4+
for i in nums:
5+
if i > max_reach:
6+
return False
7+
max_reach = max(max_reach , nums[i] + i)
8+
return True
9+
10+
11+
12+
nums = [3,2,1,0,1]
13+
print(Solution().canJump(nums))

0 commit comments

Comments
 (0)