-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.py
42 lines (40 loc) · 1.14 KB
/
solution.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
"""
56 / 56 test cases passed.
Runtime: 44 ms
Memory Usage: 19.5 MB
"""
class Solution:
def canReach(self, arr: List[int], start: int) -> bool:
if arr[start] == 0:
return True
n = len(arr)
que = collections.deque([start])
vis = [False] * n
while que:
curr = que.pop()
nxt = curr + arr[curr]
if nxt < n and not vis[nxt]:
if arr[nxt] == 0:
return True
vis[nxt] = True
que.append(nxt)
nxt = curr - arr[curr]
if nxt >= 0 and not vis[nxt]:
if arr[nxt] == 0:
return True
vis[nxt] = True
que.append(nxt)
return False
"""
56 / 56 test cases passed.
Runtime: 44 ms
Memory Usage: 19.5 MB
"""
class Solution2:
def canReach(self, arr: List[int], start: int) -> bool:
if 0 <= start < len(arr):
if arr[start] == 0: return True
temp = arr[start]
arr[start] = len(arr)
return self.canReach(arr, start + temp) or self.canReach(arr, start - temp)
return False