zoukankan      html  css  js  c++  java
  • [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;
        }
    }
  • 相关阅读:
    JQuery有几种选择器?
    Java有哪些基本数据类型
    您认为做好测试用例设计工作的关键是什么?
    final关键字
    目前主要的测试用例设计方法是什么?
    举例说明同步和异步。
    maven配置多个镜像
    参数Parameters、变量Variables
    jsp文件导包
    动态横切
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12784319.html
Copyright © 2011-2022 走看看