LeetCode 1102. Path With Maximum Minimum Value
原题链接在这里: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.
A 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 <= R, C <= 100
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的更多相关文章
- 【LeetCode】1102. Path With Maximum Minimum Value 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+并查集 优先级队列 日期 题目地址:https: ...
- LeetCode 1219. Path with Maximum Gold
原题链接在这里:https://leetcode.com/problems/path-with-maximum-gold/ 题目: In a gold mine grid of size m * n, ...
- [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 ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [学习笔记] $Maximum$ $Minimum$ $identity$
\(Maximum\) \(Minimum\) \(identity\)学习笔记 比较好玩的一个科技.具体来说就是\(max(a,b)=a+b-min(a,b)\),这个式子是比较显然的,但是这个可以 ...
随机推荐
- CORS解决跨域问题(403问题)
1.什么是跨域问题? 跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是用当前页同域名同端口的路径,这能有效的阻止跨站攻击. 2.跨域问题出现的条件: 1.跨域问题是a ...
- Word 频繁无响应
可以参考以下方法,这是我的解决办法,不保证对你也有用. 步骤一: 在「开始 > 运行」中输入「winword /a」进入无加载项 Word: 依次进入「Word 选项 > 高级 > ...
- Linux内核--伙伴系统--页释放
本文转载自:http://www.cnblogs.com/tolimit/ 感觉原博分析的不错,借花献佛. ---------------------------------------------- ...
- CentOS7安装Grafana(Yum)
一.概述 Grafana是一个跨平台的开源的分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知. 其特点: 丰富的可视化显示插件,包括热图.折线图.饼图,表格等等. 多数据源,支持 ...
- [Atcoder AGC037E]Reversing and Concatenating
题目大意:有一个长度为$n$的字符串$S$,有$k$次操作,每次操作为把$S$变为$SS^R$(即翻转后再接在一起),然后从中选取一段长度为$n$的字串.问$k$次操作后,字典序最小的一种是什么.$n ...
- C# Socket keeplive 心跳检测实例
版权声明:本文为CSDN博主「b哈利路亚d」的原创文章,重新编辑发布,请尊重原作者的劳动成果,转载的时候附上原文链接:https://blog.csdn.net/lanwilliam/article/ ...
- 在IOS10系统中无法定位问题
果是非https网页,在http协议下通过html5原生定位接口会返回错误,也就是无法正常定位到用户的具体位置,而已经支持https的网站则不会受影响. 目前提供的解决方案: 1.将网站的http设置 ...
- Mars Android 接入指南
Mars Android 接入指南 https://github.com/Tencent/mars/wiki/Mars-Android-%E6%8E%A5%E5%85%A5%E6%8C%87%E5%8 ...
- nginx反向代理、缓存及压缩配置实战
一.反向代理配置 (原文链接:http://www.studyshare.cn/blog/details/1155/0 ) 准备:两个项目分别使用端口8080,8081,只有一个备案域名,配置如下 ...
- Python进阶----进程之间通信(互斥锁,队列(参数:timeout和block),), ***生产消费者模型
Python进阶----进程之间通信(互斥锁,队列(参数:timeout和block),), ***生产消费者模型 一丶互斥锁 含义: 每个对象都对应于一个可称为" 互斥锁&qu ...