码迷,mamicode.com
首页 > 其他好文 > 详细

[LC] 1102. Path With Maximum Minimum Value

时间:2020-04-27 09:21:15      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:south   ret   mono   col   output   integer   nat   mat   jpg   

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

标签:south   ret   mono   col   output   integer   nat   mat   jpg   

原文地址:https://www.cnblogs.com/xuanlu/p/12784319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!