当前位置:网站首页>leetcode - 329. 矩阵中的最长递增路径
leetcode - 329. 矩阵中的最长递增路径
2022-04-21 20:07:00 【AttackingRookie】
思路
记忆化深搜
记录每个点的最长距离:
- 如果搜索到该点发现该点没有值,则该点记录为1
- 如果搜索到该点发现有值,则该值为搜索到点的最长路径,则在该值基础上加1即可
顺序遍历
代码
public class Solution {
// 备忘录
int[][] memo;
public int longestIncreasingPath(int[][] matrix) {
if (null == matrix || matrix.length == 0
|| null == matrix[0] || matrix[0].length == 0) {
return 0;
}
memo = new int[matrix.length][matrix[0].length];
int res = 1;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
res = Math.max(dfs(matrix, i, j, Integer.MIN_VALUE), res);
}
}
return res;
}
// 深搜
public int dfs(int[][] matrix, int i, int j, int val) {
if (i < 0 || i >= matrix.length || j < 0 || j >= matrix[i].length) {
return 0;
}
if (matrix[i][j] <= val) {
return 0;
}
if (memo[i][j] != 0) {
return memo[i][j];
}
// 能走到这,先初始化1
memo[i][j] = 1;
int top = dfs(matrix, i + 1, j, matrix[i][j]);
int bottom = dfs(matrix, i - 1, j, matrix[i][j]);
int right = dfs(matrix, i, j + 1, matrix[i][j]);
int left = dfs(matrix, i, j - 1, matrix[i][j]);
// 记忆该点最长距离
memo[i][j] = Math.max(Math.max(top, bottom), Math.max(right, left)) + 1;
return memo[i][j];
}
}
版权声明
本文为[AttackingRookie]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_39552758/article/details/124323422
边栏推荐
- How to check the slow response of the system with high CPU?
- Gbase 8A set group_ concat_ max_ Solution to error reporting after len parameter
- [gradle] problem analysis + download and installation + environment configuration + installation verification
- Ali IOT
- 解决composer报错:Could not find a version of package xxx/yyy
- 如何判斷Int型值的第nbit比特是否是1還是0
- One click installation of ROS and rosdep (no wall)
- 如何让网卡后门搞死一个系统,让你知道网卡是个多么厉害的角色
- Multi factor strategy
- webrtc入门:3.使用enumerateDevices获取设备中可用的流媒体
猜你喜欢

URL to download network resources

Digital business cloud: analyze the current situation of enterprise procurement management and promote the optimization and upgrading of enterprise procurement mode

艾尔登法环“无法加载保存数据”解决方法

如何在不加锁的情况下解决线程安全问题

表面点云法线

Solution to flash back after MySQL enters password

Comprehensive solution for digital construction of double prevention mechanism in hazardous chemical enterprises
![[线性代数]向量究竟是什么?](/img/71/1c34808b67a12cf8b402c846293f4f.png)
[线性代数]向量究竟是什么?
Practice of spark SQL in snowball

接口非幂等性解决
随机推荐
StopWatch
Im instant messaging development technology: 1-10 million high concurrency architecture evolution
在IE和Edge中用JS判断只能输入数字,字母,日期型。
Comprehensive solution for digital construction of double prevention mechanism in hazardous chemical enterprises
Install mysql, MSSQL, Oracle, mongdb and redis collections in docker
How to solve the thread safety problem without locking
华融融达期货这家公司怎么样?期货开户办理安全吗?
MySQL集群解决方案
C# 版本的 计时器类 精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出
JUC queue interface and its implementation class
全国各大城市的经纬度表,留着以后做查询库用
表面点云法线
Ali IOT
The timer class of version C is accurate to microseconds and retains one decimal place after seconds. It supports the output of year, month, day, hour, minute and second with units
Redis can send verification codes and limit the number of times sent every day
[CTF. Show. Reverse] Abstract Language
【转】Collection set map vector list 的区别和联系
PostgreSQL connection access control
浅谈多核CPU、多线程与并行计算
长安深蓝C385产品信息曝光 瞄准20万级别,头号目标Model 3!