Skip to content

Commit 33fa586

Browse files
committed
Jump Game
1 parent f6232c0 commit 33fa586

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

55-jump-game.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@
1616
Explanation: You will always arrive at index 3 no matter what. Its maximum
1717
jump length is 0, which makes it impossible to reach the last index.
1818
"""
19-
class Solution3:
20-
# Time Complexity: O(n)
21-
# Space Complexity: O(1)
19+
class Solution:
20+
def canJump(self, nums: List[int]) -> bool:
21+
max_reach = nums[0]
22+
23+
for index in range(1, len(nums)):
24+
if index > max_reach:
25+
break
26+
max_reach = max(max_reach, index + nums[index])
27+
28+
return max_reach >= len(nums) - 1
29+
30+
31+
class Solution1:
2232
def canJump(self, nums: List[int]) -> bool:
2333
lastPos = len(nums) - 1
2434
for i in range(len(nums)-1, -1, -1):
2535
if i + nums[i] >= lastPos:
2636
lastPos = i
27-
return lastPos == 0
37+
return lastPos == 0

0 commit comments

Comments
 (0)