-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#0055 Jump Game.py
More file actions
24 lines (19 loc) · 956 Bytes
/
#0055 Jump Game.py
File metadata and controls
24 lines (19 loc) · 956 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
from typing import List
class Solution:
def canJump(self, nums: List[int]) -> bool:
"""
Determines if you can reach the last index of the array.
Args:
nums (List[int]): A list of non-negative integers where each element represents the maximum jump length from that position.
Returns:
bool: True if you can reach the last index, False otherwise.
"""
target = len(nums) - 1 # The target is the last index of the array
# Iterate backward through the array starting from the second-to-last index
for i in range(len(nums) - 2, -1, -1):
# If the current index + its jump length can reach or exceed the target
if i + nums[i] >= target:
# Update the target to be the current index
target = i
# If after the loop the target is the first index, return True
return target == 0