|
| 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