原题链接在这里:https://leetcode.com/problems/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

题解:

From A[0][0], put element with index into maxHeap, sorted by element. Mark it as visited.

When polling out the currrent, check its surroundings. If not visited before, put it into maxHeap.

Until we hit the A[m-1][n-1].

Time Complexity: O(m*n*logmn). m = A.length. n = A[0].length. maxHeap add and poll takes O(logmn).

Space: O(m*n).

AC Java:

 class Solution {
int [][] dirs = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; public int maximumMinimumPath(int[][] A) {
int m = A.length;
int n = A[0].length; PriorityQueue<int []> maxHeap =
new PriorityQueue<int []>((a, b) -> b[2] - a[2]);
maxHeap.add(new int[]{0, 0, A[0][0]});
boolean [][] visited = new boolean[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(new int[]{x, y, A[x][y]});
}
} return res;
}
}

LeetCode 1102. Path With Maximum Minimum Value的更多相关文章

  1. 【LeetCode】1102. Path With Maximum Minimum Value 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+并查集 优先级队列 日期 题目地址:https: ...

  2. LeetCode 1219. Path with Maximum Gold

    原题链接在这里:https://leetcode.com/problems/path-with-maximum-gold/ 题目: In a gold mine grid of size m * n, ...

  3. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  7. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] 666. Path Sum IV 二叉树的路径和 IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  9. [学习笔记] $Maximum$ $Minimum$ $identity$

    \(Maximum\) \(Minimum\) \(identity\)学习笔记 比较好玩的一个科技.具体来说就是\(max(a,b)=a+b-min(a,b)\),这个式子是比较显然的,但是这个可以 ...

随机推荐

  1. 插件部署【BE、BP、UI】

    1.BE插件部署 XML部署位置:D:\yonyou\U9V50\Portal\bin: DLL部署位置:D:\yonyou\U9V50\Portal\ApplicationServer\Libs; ...

  2. 设置elasticsearch的默认分区数和副本数

    日志是从logstash传输给ES的,但是logstash配置中只能配置host和index,所以只能在es中进行配置 但是在es配置文件中配置,也就是新增如下参数的话会报错:node setting ...

  3. Java之数据类型讲解

    Java数据类型关系图 基本数据类型 从小到大的关系图: 图中从左向右的转换都是隐式转换,无需再代码中进行强制转换 : byte i = 12; System.out.println("by ...

  4. angular复习笔记2-架构总览

    angular架构总览 一个完整的Angular应用主要由6个重要部分构成,分别是:组件.模板.指令.服务.依赖注入和路由.这些组成部分各司其职,而又紧密协作,它们的关系如图所示. 与用户直接交互的是 ...

  5. Python中的 x+=x 与 x = x + x的区别

    对于Python中的可变数据类型(列表,字典)来说,+= 和 ..=..+..是不同的 加等是直接在变量的值上面进行操作,会修改了原来变量的值 先等后加会重新分配一个内存空间,不会再原有的变量值上面进 ...

  6. git拉取单个子目录

    初始化一个目录cron(需要拉取的的是code下的cron目录) git init cron 进入目录cd cron/ git remote add -f code ssh://git@192.168 ...

  7. 房地产propretie财产

    property 1. 财产:所有物(不可数); 地产, 房地产 He has a large property in the county. 他在这个县有一大宗地产. 1.Propretie obs ...

  8. Java 相等判断

    ==的判断机制是:根据两边的内存地址是否相同来判断. equals()是Object类的一个实例方法,判断机制和 == 完全一样. String类重写了equals()方法,是根据数据值来判断的. 总 ...

  9. unity shader入门(四):高光

    高光反射计算公式(phong模型)Cspecular=(Clight*Mspecular)max(0,v*r)mgloss mgloss为材质的官泽度,也成反射度,控制高光区域亮点有多大 Mspecu ...

  10. Java 单文件、多文件上传 / 实现上传进度条

    博客地址:https://ainyi.com/76 日常,工作 在这里总结一下上传吧(是以前做过的练习,就汇总到个人博客吧) java ssm 框架实现文件上传 实现:单文件上传.多文件上传(单选和多 ...