Skip to content

Commit c11ef40

Browse files
authored
Create jump-game-iii.py
1 parent 239acb4 commit c11ef40

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Python/jump-game-iii.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def canReach(self, arr, start):
9+
"""
10+
:type arr: List[int]
11+
:type start: int
12+
:rtype: bool
13+
"""
14+
q, lookup = collections.deque([start]), set([start])
15+
while q:
16+
i = q.popleft()
17+
if not arr[i]:
18+
return True
19+
for j in [i-arr[i], i+arr[i]]:
20+
if 0 <= j < len(arr) and j not in lookup:
21+
lookup.add(j)
22+
q.append(j)
23+
return False

0 commit comments

Comments
 (0)