Path With Maximum Minimum Value

2024-09-04 14:18
文章标签 maximum path value minimum

本文主要是介绍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

思路:破题思路,想最后一个点,上边和左边过来,每条路径的value是这条路径上的最小,但是我首先是跟上边和左边过来的最大值进行比较,所以pq是为了处理每次四个方向扩散,首先扩散最大的value,这题跟waterII很类似,water是每次过来取四周最小值,先进行扩散,这里相反,每次取最大值进行扩散,T: O(mn*lg(mn)); 注意,dijkstra的算法,visited一定要在外面,而不是if block里面进行判断,否则会漏掉最优解;

class Solution {public class Node {public int x;public int y;public int value;public Node(int x, int y, int value) {this.x = x;this.y = y;this.value = value;}}public int maximumMinimumPath(int[][] grid) {int m = grid.length;int n = grid[0].length;PriorityQueue<Node> pq = new PriorityQueue<Node>((a, b) -> (b.value - a.value));pq.offer(new Node(0, 0, grid[0][0]));int[][] dirs = {{0,1},{0,-1},{-1,0},{1,0}};boolean[][] visited = new boolean[m][n];while(!pq.isEmpty()) {Node node = pq.poll();if(node.x == m - 1 && node.y == n - 1) {return node.value;}if(visited[node.x][node.y]) {continue;}visited[node.x][node.y] = true;for(int[] dir: dirs) {int nx = node.x + dir[0];int ny = node.y + dir[1];if(0 <= nx && nx < m && 0 <= ny && ny < n && !visited[nx][ny]) {pq.offer(new Node(nx, ny, Math.min(grid[nx][ny], node.value)));}}}return -1;}
}

这篇关于Path With Maximum Minimum Value的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136215

相关文章

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

python中os.stat().st_size、os.path.getsize()获取文件大小

《python中os.stat().st_size、os.path.getsize()获取文件大小》本文介绍了使用os.stat()和os.path.getsize()函数获取文件大小,文中通过示例代... 目录一、os.stat().st_size二、os.path.getsize()三、函数封装一、os

【Hdu】Minimum Inversion Number(逆序,线段树)

利用线段树在nlogn的时间复杂度内求一段数的逆序。 由于给的序列是由0 ~ n -1组成的,求出初始的逆序之后可以递推出移动之后的逆序数。 #include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const in

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

Flask 创建app 时候传入的 static_folder 和 static_url_path参数理解

Flask 在创建app的时候 是用 app = Flask(__name__) 来创建的,不传入 static_folder参数的话 ,默认的静态文件的位置是在 static目录下 我们可以进入 Flask的源码里面查看 ctrl+鼠标左键进入 这是Flask的 __init__源码(后面还有一些,我就选了需要的代码)     def __init__(self,import_

为 Key-Value 数据库实现MVCC 事务

ACID是软件领域使用最广泛的技术之一,它是关系数据库的基石,是企业级中间件不可或缺的部分,但通常通过黑盒的方式提供。但是在许多情况下,这种古老的事务方式已经不能够适应现代大规模系统和NoSQL数据库的需要了,现代系统要求更高的性能要求,更大的数据量,更高的可用性。在这种情况下,传统的事务模型被定制的事务或者半事务模型所取代,而在这些模型中事务性并不像以往那样被看重。   在本文中我们会讨论一