[LC] 1102. Path With Maximum Minimum Value

Posted xuanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LC] 1102. Path With Maximum Minimum Value相关的知识,希望对你有一定的参考价值。

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.

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

 

Example 1:

技术图片

Input: [[5,4,5],[1,2,6],[7,4,6]]
Output: 4
Explanation: 
The path with the maximum score is highlighted in yellow. 

Example 2:

技术图片

Input: [[2,2,1,2,2,2],[1,2,2,2,1,2]]
Output: 2

Example 3:

技术图片

Input: [[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0],[4,6,5,4,3]]
Output: 3

 

Note:

  1. 1 <= R, C <= 100
  2. 0 <= A[i][j] <= 10^9

Time: O(M * N * log(M * N))

class Solution {
    int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int ROW;
    int COL;
    public int maximumMinimumPath(int[][] A) {
        ROW = A.length;
        COL = A[0].length;
        boolean[][] visited = new boolean[ROW][COL];
        int minValue = A[0][0];
        // find the max value for the nearby cells
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[2] - a[2]);
        pq.offer(new int[]{0, 0, A[0][0]});
        while (!pq.isEmpty()) {
            int[] cur = pq.poll();
            int curRow = cur[0];
            int curCol = cur[1];
            minValue = Math.min(minValue, cur[2]);
            if (curRow == ROW - 1 && curCol == COL - 1) {
                break;
            }
            for (int[] dir: directions) {
                int nextRow = curRow + dir[0];
                int nextCol = curCol + dir[1];
                if (nextRow < 0 || nextRow >= ROW || nextCol < 0 || nextCol >= COL || visited[nextRow][nextCol]) {
                    continue;
                }
                pq.offer(new int[]{nextRow, nextCol, A[nextRow][nextCol]});
                visited[nextRow][nextCol] = true;
            }
        }
        return minValue;
    }
}

 

以上是关于[LC] 1102. Path With Maximum Minimum Value的主要内容,如果未能解决你的问题,请参考以下文章

LC 992. Subarrays with K Different Integers

LC 967. Numbers With Same Consecutive Differences

[LC] 373. Find K Pairs with Smallest Sums

lc 805. Split Array With Same Average

LC.112.Path Sum

[LC] 112. Path Sum