Skip to content

Commit 1423d57

Browse files
committed
update readme
1 parent c7ccf26 commit 1423d57

File tree

5 files changed

+44
-155
lines changed

5 files changed

+44
-155
lines changed

0505-the-maze-ii/0505-the-maze-ii.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

0505-the-maze-ii/README.md

Lines changed: 0 additions & 48 deletions
This file was deleted.

Python/0505-the-maze-ii.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
1-
from collections import deque
2-
import heapq
1+
# time complexity: O(m*n*log(m*n))
2+
# space complexity: O(m*n)
3+
from collections import defaultdict
4+
from heapq import heappop, heappush
35
from typing import List
46

57

68
class Solution:
79
def shortestDistance(self, maze: List[List[int]], start: List[int], destination: List[int]) -> int:
8-
m, n, q, stopped = len(maze), len(maze[0]), [(0, start[0], start[1])], {
9-
(start[0], start[1]): 0}
10-
11-
while q:
12-
dist, x, y = heapq.heappop(q)
13-
if [x, y] == destination:
14-
return dist
15-
for i, j in ((-1, 0), (1, 0), (0, -1), (0, 1)):
16-
newX, newY, d = x, y, 0
17-
while 0 <= newX + i < m and 0 <= newY + j < n and maze[newX + i][newY+j] != 1:
18-
newX += i
19-
newY += j
20-
d += 1
21-
22-
if (newX, newY) not in stopped or dist + d < stopped[(newX, newY)]:
23-
stopped[(newX, newY)] = dist + d
24-
heapq.heappush(q, (dist + d, newX, newY))
10+
ROW = len(maze)
11+
COL = len(maze[0])
12+
visited = defaultdict(lambda: float('inf'))
13+
minHp = [(0, start[0], start[1])]
14+
visited[(start[0], start[1])] = 0
15+
while minHp:
16+
currDist, currR, currC = heappop(minHp)
17+
if [currR, currC] == destination:
18+
return currDist
19+
for dR, dC in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
20+
nextR = currR
21+
nextC = currC
22+
nextDist = currDist
23+
while 0 <= nextR + dR < ROW and 0 <= nextC + dC < COL and maze[nextR + dR][nextC + dC] == 0:
24+
nextR += dR
25+
nextC += dC
26+
nextDist += 1
27+
if nextDist < visited[(nextR, nextC)]:
28+
heappush(minHp, (nextDist, nextR, nextC))
29+
visited[(nextR, nextC)] = nextDist
2530
return -1
2631

2732

2833
maze = [[0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [
2934
0, 0, 0, 1, 0], [1, 1, 0, 1, 1], [0, 0, 0, 0, 0]]
3035
start = [0, 4]
3136
destination = [4, 4]
32-
37+
print(Solution().shortestDistance(maze, start, destination))
38+
maze = [[0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [
39+
0, 0, 0, 1, 0], [1, 1, 0, 1, 1], [0, 0, 0, 0, 0]]
40+
start = [0, 4]
41+
destination = [3, 2]
42+
print(Solution().shortestDistance(maze, start, destination))
43+
maze = [[0, 0, 0, 0, 0], [1, 1, 0, 0, 1], [
44+
0, 0, 0, 0, 0], [0, 1, 0, 0, 1], [0, 1, 0, 0, 0]]
45+
start = [4, 3]
46+
destination = [0, 1]
3347
print(Solution().shortestDistance(maze, start, destination))

README.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,3 @@ It helps others discover the repo and keeps the project growing.
115115
---
116116

117117
Feedback / Questions → open an Issue or reach out on [LinkedIn](https://www.linkedin.com/in/hogan-l/)
118-
119-
<!---LeetCode Topics Start-->
120-
# LeetCode Topics
121-
## Array
122-
| |
123-
| ------- |
124-
| [0505-the-maze-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0505-the-maze-ii) |
125-
## Depth-First Search
126-
| |
127-
| ------- |
128-
| [0505-the-maze-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0505-the-maze-ii) |
129-
## Breadth-First Search
130-
| |
131-
| ------- |
132-
| [0505-the-maze-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0505-the-maze-ii) |
133-
## Graph
134-
| |
135-
| ------- |
136-
| [0505-the-maze-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0505-the-maze-ii) |
137-
## Heap (Priority Queue)
138-
| |
139-
| ------- |
140-
| [0505-the-maze-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0505-the-maze-ii) |
141-
## Matrix
142-
| |
143-
| ------- |
144-
| [0505-the-maze-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0505-the-maze-ii) |
145-
## Shortest Path
146-
| |
147-
| ------- |
148-
| [0505-the-maze-ii](https://github.com/hogan-tech/leetcode-solution/tree/master/0505-the-maze-ii) |
149-
<!---LeetCode Topics End-->

Readme/0505-the-maze-ii.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
<h2><a href="https://leetcode.com/problems/the-maze-ii/">505. The Maze II</a></h2><h3>Medium</h3><hr><div><p>There is a ball in a <code>maze</code> with empty spaces (represented as <code>0</code>) and walls (represented as <code>1</code>). The ball can go through the empty spaces by rolling <strong>up, down, left or right</strong>, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.</p>
1+
<h2><a href="https://leetcode.com/problems/the-maze-ii">505. The Maze II</a></h2><h3>Medium</h3><hr><p>There is a ball in a <code>maze</code> with empty spaces (represented as <code>0</code>) and walls (represented as <code>1</code>). The ball can go through the empty spaces by rolling <strong>up, down, left or right</strong>, but it won&#39;t stop rolling until hitting a wall. When the ball stops, it could choose the next direction.</p>
22

3-
<p>Given the <code>m x n</code> <code>maze</code>, the ball's <code>start</code> position and the <code>destination</code>, where <code>start = [start<sub>row</sub>, start<sub>col</sub>]</code> and <code>destination = [destination<sub>row</sub>, destination<sub>col</sub>]</code>, return <em>the shortest <strong>distance</strong> for the ball to stop at the destination</em>. If the ball cannot stop at <code>destination</code>, return <code>-1</code>.</p>
3+
<p>Given the <code>m x n</code> <code>maze</code>, the ball&#39;s <code>start</code> position and the <code>destination</code>, where <code>start = [start<sub>row</sub>, start<sub>col</sub>]</code> and <code>destination = [destination<sub>row</sub>, destination<sub>col</sub>]</code>, return <em>the shortest <strong>distance</strong> for the ball to stop at the destination</em>. If the ball cannot stop at <code>destination</code>, return <code>-1</code>.</p>
44

55
<p>The <strong>distance</strong> is the number of <strong>empty spaces</strong> traveled by the ball from the start position (excluded) to the destination (included).</p>
66

77
<p>You may assume that <strong>the borders of the maze are all walls</strong> (see examples).</p>
88

99
<p>&nbsp;</p>
1010
<p><strong class="example">Example 1:</strong></p>
11-
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/31/maze1-1-grid.jpg" style="width: 573px; height: 573px;">
12-
<pre><strong>Input:</strong> maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4]
11+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/31/maze1-1-grid.jpg" style="width: 573px; height: 573px;" />
12+
<pre>
13+
<strong>Input:</strong> maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4]
1314
<strong>Output:</strong> 12
1415
<strong>Explanation:</strong> One possible way is : left -&gt; down -&gt; left -&gt; down -&gt; right -&gt; down -&gt; right.
1516
The length of the path is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
1617
</pre>
1718

1819
<p><strong class="example">Example 2:</strong></p>
19-
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/31/maze1-2-grid.jpg" style="width: 573px; height: 573px;">
20-
<pre><strong>Input:</strong> maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2]
20+
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/31/maze1-2-grid.jpg" style="width: 573px; height: 573px;" />
21+
<pre>
22+
<strong>Input:</strong> maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2]
2123
<strong>Output:</strong> -1
2224
<strong>Explanation:</strong> There is no way for the ball to stop at the destination. Notice that you can pass through the destination but you cannot stop there.
2325
</pre>
2426

2527
<p><strong class="example">Example 3:</strong></p>
2628

27-
<pre><strong>Input:</strong> maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], start = [4,3], destination = [0,1]
29+
<pre>
30+
<strong>Input:</strong> maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], start = [4,3], destination = [0,1]
2831
<strong>Output:</strong> -1
2932
</pre>
3033

@@ -43,4 +46,3 @@ The length of the path is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
4346
<li>Both the ball and the destination exist in an empty space, and they will not be in the same position initially.</li>
4447
<li>The maze contains <strong>at least 2 empty spaces</strong>.</li>
4548
</ul>
46-
</div>

0 commit comments

Comments
 (0)