Skip to content

Commit d97db06

Browse files
solves #499: The maze III in java
1 parent afaac25 commit d97db06

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@
334334
| 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle) | [![Java](assets/java.png)](src/ConstructTheRectangle.java) [![Python](assets/python.png)](python/construct_the_rectangle.py) | |
335335
| 495 | [Teemo Attacking](https://leetcode.com/problems/teemo-attacking/) | [![Java](assets/java.png)](src/TeemoAttacking.java) [![Python](assets/python.png)](python/teemo_attacking.py) | |
336336
| 496 | [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i) | [![Java](assets/java.png)](src/NextGreaterElementI.java) [![Python](assets/python.png)](python/next_greater_element_i.py) | |
337+
| 499 | [The Maze III](https://leetcode.com/problems/the-maze-iii) | [![Java](assets/java.png)](src/TheMazeIII.java) | |
337338
| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row) | [![Java](assets/java.png)](src/KeyBoardRow.java) [![Python](assets/python.png)](python/keyboard_row.py) | |
338339
| 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) | |
339340
| 504 | [Base 7](https://leetcode.com/problems/base-7) | [![Java](assets/java.png)](src/Base7.java) [![Python](assets/python.png)](python/base_7.py) | |

src/TheMazeIII.java

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// https://leetcode.com/problems/the-maze-iii
2+
// T: O(m * n log(m * n))
3+
// S: O(m * n)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.PriorityQueue;
9+
import java.util.Queue;
10+
11+
public class TheMazeIII {
12+
private record Info(int[] position, int distance, String path) {}
13+
14+
private record Position(int[] position, String direction) {}
15+
16+
private static final Position[] DIRECTIONS = new Position[] {
17+
new Position(new int[] { 1, 0 }, "d"),
18+
new Position(new int[] { 0, -1 }, "l"),
19+
new Position(new int[] { 0, 1 }, "r"),
20+
new Position(new int[] { -1, 0 }, "u")
21+
};
22+
23+
// T: O(m * n * log(m * n)), S: O(m * n)
24+
public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
25+
final int[][] distances = getDistances(maze.length, maze[0].length);
26+
final Queue<Info> queue = new PriorityQueue<>((a, b) -> {
27+
if (a.distance == b.distance) {
28+
return a.path.compareTo(b.path);
29+
}
30+
return Integer.compare(a.distance, b.distance);
31+
});
32+
queue.add(new Info(ball, 0, ""));
33+
String path = null;
34+
35+
while (!queue.isEmpty()) {
36+
final Info info = queue.poll();
37+
38+
if (distances[info.position[0]][info.position[1]] <= info.distance) {
39+
continue;
40+
}
41+
42+
distances[info.position[0]][info.position[1]] = info.distance;
43+
44+
if (info.position[0] == hole[0] && info.position[1] == hole[1]) {
45+
if (path == null) {
46+
path = info.path;
47+
} else {
48+
path = path.compareTo(info.path) > 0 ? info.path : path;
49+
}
50+
}
51+
52+
for (Position position : validPositions(maze, info.position, hole)) {
53+
queue.add(new Info(
54+
position.position,
55+
info.distance + manhattanDistance(position.position, info.position),
56+
info.path + position.direction
57+
));
58+
}
59+
}
60+
61+
final int minDistance = distances[hole[0]][hole[1]];
62+
return minDistance == Integer.MAX_VALUE ? "impossible" : path;
63+
}
64+
65+
// T: O(1), S:O(1)
66+
private static int manhattanDistance(int[] p1, int[] p2) {
67+
return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);
68+
}
69+
70+
// T: O(m + n), S: O(1)
71+
private static List<Position> validPositions(int[][] grid, int[] position, int[] target) {
72+
final List<Position> result = new ArrayList<>();
73+
for (Position p : DIRECTIONS) {
74+
int row = position[0], column = position[1];
75+
while (row >= 0 && row < grid.length
76+
&& column >= 0 && column < grid[0].length && grid[row][column] == 0 &&
77+
(row != target[0] || column != target[1])) {
78+
row += p.position[0];
79+
column += p.position[1];
80+
}
81+
if (row == target[0] && column == target[1]) {
82+
result.add(new Position(new int[] { row, column}, p.direction));
83+
} else {
84+
result.add(new Position(new int[]{row - p.position[0], column - p.position[1]}, p.direction));
85+
}
86+
}
87+
return result;
88+
}
89+
90+
// T: O(m * n), S: O(m * n)
91+
private static int[][] getDistances(int rows, int columns) {
92+
final int[][] distances = new int[rows][columns];
93+
for (int[] row : distances) {
94+
Arrays.fill(row, Integer.MAX_VALUE);
95+
}
96+
return distances;
97+
}
98+
}

0 commit comments

Comments
 (0)