Skip to content

Commit afaac25

Browse files
solves maze
1 parent 6a9916f commit afaac25

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@
337337
| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row) | [![Java](assets/java.png)](src/KeyBoardRow.java) [![Python](assets/python.png)](python/keyboard_row.py) | |
338338
| 501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree) | [![Java](assets/java.png)](src/FindModeInBinarySearchTree.java) [![Python](assets/python.png)](python/find_mode_in_binary_search_tree.py) | |
339339
| 504 | [Base 7](https://leetcode.com/problems/base-7) | [![Java](assets/java.png)](src/Base7.java) [![Python](assets/python.png)](python/base_7.py) | |
340+
| 505 | [The Maze II](https://leetcode.com/problems/the-maze-ii) | [![Java](assets/java.png)](src/TheMazeII.java) | |
340341
| 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks) | [![Java](assets/java.png)](src/RelativeRanks.java) [![Python](assets/python.png)](python/relative_ranks.py) | |
341342
| 507 | [Perfect Number](https://leetcode.com/problems/perfect-number) | [![Java](assets/java.png)](src/CheckPerfectNumber.java) [![Python](assets/python.png)](python/check_perfect_number.py) | |
342343
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number) | [![Java](assets/java.png)](src/FibonacciNumber.java) [![Python](assets/python.png)](python/fibonacci_number.py) | |

Diff for: src/TheMazeII.java

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// https://leetcode.com/problems/the-maze-ii
2+
// T: O(m * n * log(m * n))
3+
// S: O(m * n)
4+
5+
import java.util.ArrayList;
6+
import java.util.Comparator;
7+
import java.util.List;
8+
import java.util.PriorityQueue;
9+
import java.util.Queue;
10+
11+
public class TheMazeII {
12+
private static final int[][] DIRECTIONS = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
13+
14+
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
15+
final int rows = maze.length, columns = maze[0].length;
16+
final int[][] distance = dijkstra(maze, start);
17+
return distance[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : distance[destination[0]][destination[1]];
18+
}
19+
20+
// T: O(m * n * log(mn))
21+
// S: O(m * n)
22+
public int[][] dijkstra(int[][] maze, int[] start) {
23+
final int[][] distance = getDistances(maze.length, maze[0].length);
24+
final Queue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(a -> a[2]));
25+
26+
queue.add(new int[] { start[0], start[1], 0 });
27+
28+
while (!queue.isEmpty()) {
29+
final int[] s = queue.poll();
30+
final int row = s[0], column = s[1], d = s[2];
31+
32+
if (distance[row][column] <= d) {
33+
continue;
34+
}
35+
36+
distance[row][column] = d;
37+
38+
for (int[] position: validPositions(maze, s[0], s[1])) {
39+
final int newDistance = distance[row][column] + manhattanDistance(s[0], s[1], position);
40+
queue.add(new int[] { position[0], position[1], newDistance});
41+
}
42+
}
43+
44+
return distance;
45+
}
46+
47+
// T: O(m + n)
48+
// S: O(1)
49+
private static List<int[]> validPositions(int[][] grid, int row, int column) {
50+
final List<int[]> result = new ArrayList<>();
51+
for (int[] direction : DIRECTIONS) {
52+
int i = row, j = column;
53+
while (i >= 0 && j >= 0 && i < grid.length && j < grid[0].length && grid[i][j] == 0) {
54+
i += direction[0];
55+
j += direction[1];
56+
}
57+
result.add(new int[] { i - direction[0], j - direction[1] });
58+
}
59+
return result;
60+
}
61+
62+
// T: O(m * n)
63+
// S: O(m * n)
64+
private static int[][] getDistances(int rows, int columns) {
65+
final int[][] result = new int[rows][columns];
66+
for (int i = 0 ; i < rows ; i++) {
67+
for (int j = 0 ; j < columns ; j++) {
68+
result[i][j] = Integer.MAX_VALUE;
69+
}
70+
}
71+
return result;
72+
}
73+
74+
// T: O(1), S: O(1)
75+
private static int manhattanDistance(int row, int column, int[] position) {
76+
return Math.abs(row - position[0]) + Math.abs(column - position[1]);
77+
}
78+
}

0 commit comments

Comments
 (0)