Given a matrix of integers A with R rows and C columns, find the maximum score of a path starting at [0,0] and ending at [R-1,C-1].
The score of a path is the minimum value in that path. For example, the value of the path 8 โ 4 โ 5 โ 9 is 4.
A path moves some number of times from one visited cell to any neighbouring unvisited cell in one of the 4 cardinal directions (north, east, west, south).
ไปฃ็
Approach #1 Priority BFS Greedy
classSolution {int [][] dirs = {{0,-1}, {0,1}, {-1,0}, {1,0}};publicintmaximumMinimumPath(int[][] A) {int m =A.length;int n =A[0].length;PriorityQueue<int[]> maxHeap =newPriorityQueue<int[]>((a, b) -> b[2] - a[2]);maxHeap.add(newint[]{0,0,A[0][0]});boolean [][] visited =newboolean[m][n]; visited[0][0] =true;int res =A[0][0];while (!maxHeap.isEmpty()) {int[] cur =maxHeap.poll(); res =Math.min(res, cur[2]);if (cur[0] == m -1&& cur[1] == n -1) {return res; }for (int [] dir : dirs) {int x = cur[0] + dir[0];int y = cur[1] + dir[1];if (x <0|| x >= m || y <0|| y >= n || visited[x][y]) {continue; } visited[x][y] =true;maxHeap.add(newint[]{x, y,A[x][y]}); } }return res; }}