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

相关文章

【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数据库的需要了,现代系统要求更高的性能要求,更大的数据量,更高的可用性。在这种情况下,传统的事务模型被定制的事务或者半事务模型所取代,而在这些模型中事务性并不像以往那样被看重。   在本文中我们会讨论一

(4)SVG-path中的椭圆弧A(绝对)或a(相对)

1、概念 表示经过起始点(即上一条命令的结束点),到结束点之间画一段椭圆弧 2、7个参数 rx,ry,x-axis-rotation,large-arc-flag,sweep-flag,x,y (1)和(2)rx,ry rx:椭圆的x轴半径(即水平半径) ry:椭圆的y轴半径(即垂直半径) 这两个参数好理解,就是椭圆的两条对称轴半径,相等即为圆 也可以写比例,写比例时默认用符合条件

兔子-(PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recommended: 'CGP'

由于在PHP最新的版本中增加了一个配置项目“request_order”,默认值为“GP”,这个存在一定的安全风险。这里我们建议用户将配置更改为“CGP” 可以在php的安装目录下找到php.ini配置目录,找到下面选项: request_order = "GP"  更改为 request_order = "CGP"   重启服务器后即可。 此

MySql 1264 - Out of range value for column 异常

前段时间操作数据库,本是一个很简单的修改语句,却报了  1264 - Out of range value for column字段类型官网  当时一看懵逼了,网上很多都说是配置的问题,需要修改my.ini文件,这个方式我没有试过,我想肯定还有其它方法,经过慢慢排 查发现表里的字段为 decimal(10,3) ,这说明小数点前只有7位,保留了3位小数点,而值在小数点前却有8位,这就导致了错误

【ArcGIS Pro实操第二期】最小成本路径(Least-cost path)原理及实操案例

ArcGIS Pro实操第一期:最小成本路径原理及实操案例 概述(Creating the least-cost path)1.1 原理介绍1.2 实现步骤1.3 应用案例 2 GIS实操2.1 工具箱简介2.1.1 成本路径(Cost path)2.1.2 成本距离(Cost distance)2.1.2 路径距离(Path Distance) 2.2 案例: 参考 概述(Cre

Maximum likelihood function maximizes what thing?

最大似然函数(Maximum Likelihood Function)最大化的是数据在给定参数下出现的概率。具体来说,它最大化的是似然函数(Likelihood Function),即给定参数 ( \theta ) 下观测数据的概率。在统计学中,似然函数 ( L(\theta) ) 通常定义为所有独立观测数据点概率的乘积,对于参数 ( \theta ) 的函数。 对于一组独立同分布的观测数据

大数据Java基础-JAVA IO 9】java IO流 (九) Path、Paths、Files的使用

1.NIO的使用说明: >Java NIO (New IO,Non-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API,可以替代标准的Java IO AP。 >NIO与原来的IO同样的作用和目的,但是使用的方式完全不同,NIO支持面向缓冲区的(IO是面向流的)、基于通道的IO操作。 >NIO将以更加高效的方式进行文件的读写操作。 >随着 JDK 7 的发布,Java对N