GuilinDev

Lc1102

05 August 2008

1102. Path With Maximum Minimum Value

给定一个 m x n 整数矩阵网格,返回从 (0, 0) 开始到 (m - 1, n - 1) 结束的路径在 4 个基本方向上移动的最大分数。

路径的分数是该路径中的最小值。

例如,路径 8 → 4 → 5 → 9 的得分为 4。

BFS + Heap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
    public int maximumMinimumPath(int[][] grid) {

        Queue<int[]> queue = new PriorityQueue<>((a, b) -> b[2] - a[2]);

        queue.offer(new int[]{0, 0, grid[0][0]});

        int[][] dirs = new int[][]{ {0, 1}, {1, 0}, {-1, 0}, {0, -1} };
        boolean[][] visited = new boolean[grid.length][grid[0].length];
        visited[0][0] = true;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int[] state = queue.poll();
                if (state[0] == grid.length - 1 && state[1] == grid[0].length - 1)
                    return state[2];

                for (int[] dir : dirs) {
                    int x = state[0] + dir[0];
                    int y = state[1] + dir[1];
                    if (x < 0 || y < 0 || x == grid.length || y == grid[0].length || visited[x][y]) {
                        continue;
                    }
                    queue.offer(new int[]{x, y, Math.min(state[2], grid[x][y])});
                    visited[x][y] = true;
                }
            }

        }
        return -1;
    }
}