|
1 | 1 | class Solution {
|
2 |
| - int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; |
3 |
| - public int maximumMinimumPath(int[][] A) { |
4 |
| - int rows = A.length; |
5 |
| - int cols = A[0].length; |
6 |
| - boolean[][] visited = new boolean[rows][cols]; |
7 |
| - PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>(){ |
8 |
| - public int compare(int[] p1, int[] p2) { |
9 |
| - return p2[0] - p1[0]; |
10 |
| - } |
11 |
| - }); |
12 |
| - int maxVal = A[0][0]; |
13 |
| - pq.add(new int[]{A[0][0], 0, 0}); |
14 |
| - visited[0][0] = true; |
| 2 | + |
| 3 | + private static final int[][] DIRS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; |
| 4 | + |
| 5 | + public int maximumMinimumPath(int[][] grid) { |
| 6 | + PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> o2[2] - o1[2]); |
| 7 | + pq.add(new int[]{0, 0, grid[0][0]}); |
| 8 | + int numRows = grid.length; |
| 9 | + int numCols = grid[0].length; |
| 10 | + boolean[][] visited = new boolean[numRows][numCols]; |
15 | 11 | while (!pq.isEmpty()) {
|
16 | 12 | int[] removed = pq.poll();
|
17 |
| - int x = removed[1]; |
18 |
| - int y = removed[2]; |
19 |
| - maxVal = Math.min(maxVal, removed[0]); |
20 |
| - if (x == rows - 1 && y == cols - 1) { |
21 |
| - break; |
| 13 | + if (removed[0] == numRows - 1 && removed[1] == numCols - 1) { |
| 14 | + return removed[2]; |
22 | 15 | }
|
23 |
| - for (int[] dir : dirs) { |
24 |
| - int newX = x + dir[0]; |
25 |
| - int newY = y + dir[1]; |
26 |
| - if (newX >= 0 && newY >= 0 && newX < rows && newY < cols && !visited[newX][newY]) { |
27 |
| - pq.add(new int[]{A[newX][newY], newX, newY}); |
28 |
| - visited[newX][newY] = true; |
| 16 | + visited[removed[0]][removed[1]] = true; |
| 17 | + for (int[] dir : DIRS) { |
| 18 | + int newX = removed[0] + dir[0]; |
| 19 | + int newY = removed[1] + dir[1]; |
| 20 | + if (newX >= 0 && newY >= 0 && newX < numRows && newY < numCols && !visited[newX][newY]) { |
| 21 | + pq.add(new int[]{newX, newY, Math.min(removed[2], grid[newX][newY])}); |
29 | 22 | }
|
30 | 23 | }
|
31 | 24 | }
|
32 |
| - return maxVal; |
| 25 | + return -1; |
33 | 26 | }
|
34 | 27 | }
|
0 commit comments