-
Notifications
You must be signed in to change notification settings - Fork 0
/
106.py
35 lines (25 loc) · 777 Bytes
/
106.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
"""
Problem:
Given an integer list where each number represents the number of hops you can make,
determine whether you can reach to the last index starting at index 0.
For example, [2, 0, 1, 0] returns true while [1, 1, 0, 1] returns false.
"""
from typing import List
def can_reach_end(arr: List[int]) -> bool:
length = len(arr)
curr_position, last_index = 0, length - 1
while curr_position < length:
if curr_position == last_index:
return True
elif arr[curr_position] == 0:
return False
curr_position += arr[curr_position]
return False
if __name__ == "__main__":
print(can_reach_end([2, 0, 1, 0]))
print(can_reach_end([1, 1, 0, 1]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
"""