-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1306.Jump-Game-III-v2.py
30 lines (27 loc) · 1.05 KB
/
1306.Jump-Game-III-v2.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
from collections import deque
class Solution:
def canReach(self, arr: List[int], start: int) -> bool:
length = len(arr)
visited = [False] * length
# Run BFS
q = deque([start])
visited[start] = True
if arr[start] == 0:
return True
while q:
sz = len(q)
for _ in range(sz):
cur_pos = q.popleft()
for next_pos in [cur_pos + arr[cur_pos], cur_pos - arr[cur_pos]]:
if next_pos >= length or next_pos < 0:
# We cannot jump outside of the array
continue
if not visited[next_pos]:
# Early termination if we reach
# a position with a 0 value
if arr[next_pos] == 0:
return True
visited[next_pos] = True
q.append(next_pos)
# Check whether all enries with value 0 is visited
return False