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)\),这个式子是比较显然的,但是这个可以 ...
随机推荐
- QT5的QChart使用记录
如果需要在QT中使用QChart类,需要在安装的时候勾选QChart选项,在工程的 .pro 文件里面添加 QT += charts 语句,包含 QChart 头文件就行了. 对于图表的显示,可以先拖 ...
- svn-疑难解决
Linux下搭建SVN服务器 https://blog.51cto.com/19940919/2095011 [SVN]出现SVN--Authorization failed错误原因 https:// ...
- scratch少儿编程第一季——09、声音模块:吹拉弹唱我也会
各位小伙伴大家好: 上期我们学习了外观模块的指令,学会了制作特效. 本期我们来学习如何给游戏配音. 声音模块的指令不是很多,我们一起来看看吧. 首先第一个就是播放声音,里面默认插入了喵叫声. 我们点击 ...
- 复杂sql语句之单字段分类count计数和多字段count计数
SELECT AF_DEPARTMENT, dept.FULLNAME, SUM(CASE AF_CLASSIFY WHEN THEN ELSE END) AS 'o_standard', (COUN ...
- string.Compare()方法
判断字符串中是否包含一个值 返回一个值,该值指示指定的 String 对象是否出现在此字符串中. String a = "abcd"; if(source.a("a&qu ...
- CentOS7配置网卡上网、安装wget、配置163yum源
2019/09/12,CentOS 7 VMware 摘要:CentOS7安装完成(最小化安装)后,不能联网(已选择桥接网络),需要修改配置文件及配置yum源 修改配置文件 进入网卡配置目录 cd / ...
- 使用Filezilla Server配置FTP服务器
一.下载Filezilla Server 官网网址:https://filezilla-project.org 二.安装Filezilla Server Filezilla Server的安 ...
- python中的{字典}
目录 字典--dict { } 字典是无序,可变的数据类型. 字典:用于存储数据,存储大量数据,字典要比列表快:将数据和数据之间进行关联. 定义: dic = {键:值,键:值} #每个 键值对 以逗 ...
- Java JAR包
JAR文件全称 Java Archive File,意为Java档案文件.JAR文件是一种压缩文件,也被成为JAR包. 运行程序时,JVM会自动在内存中解压要用的JAR包. 使用JAR包的优点:1.安 ...
- django中使用AJAX时如何获取表单参数(按钮携带参数)
前提是函数和相应的视图路由都已经配置好了,然后就是表单了: <form id="SmsForm" method="post" class="a& ...