当前位置:网站首页>leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
2022-07-01 12:35:00 【白速龙王的回眸】

分析
为什么不用visit,因为是严格递增序列,所以一定不存在还能回头走的情况,所以无需visit
然后思路也是一样,遍历每个起点
dfs返回的是最大长度,记录的参数包括当前的位置,以及前一个数的位置
由于我们遍历的是起点,所以天然的,前一个数的位置就是所选的起点
然后由于如果我们固定了当前的位置,以及前一个数的大小,这个能走的最大距离也是固定的
因此可以记忆化
这里面的核心代码就是,把现在的难题丢给下一步(下一步满足条件)
res = max(res, dfs(nr, nc, matrix[nr][nc]) + 1)
ac code
class Solution:
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
m, n = len(matrix), len(matrix[0])
maxn = 0
# dfs + memory优雅
# 当点和上一个点固定,它能走的最大长度也就固定
@cache
def dfs(r, c, pre):
res = 1 # 最少也是当前这个点
for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
if 0 <= nr < m and 0 <= nc < n and matrix[nr][nc] > pre:
res = max(res, dfs(nr, nc, matrix[nr][nc]) + 1)
return res
ans = 0
# 遍历每个起点获得不同起点的最大值
for i in range(m):
for j in range(n):
ans = max(ans, dfs(i, j, matrix[i][j]))
return ans
总结
二维图求最什么的路径啥的
可以考虑dfs + cache
dfs记录的是啥,参数是啥,输出是啥
什么时候能进入下一个dfs
上一个dfs和下一个dfs有什么关系
需要visit防止重复吗
需要回溯吗。。。
边栏推荐
- 被锡膏坑了一把
- ANSI/UL 94 VTM薄质材料垂直燃烧测试
- [Suanli network] technological innovation of Suanli Network -- key technology of operation service
- Question d'entrevue de Huawei: recrutement
- ASP.NET Core 6 从入门到企业级实战开发应用技术汇总
- localtime居然不可重入,踩坑了
- Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
- 队列操作---
- 网络socket的状态要怎么统计?
- MySQL workbench data modeling function
猜你喜欢

Sort out relevant contents of ansible

Stack-------

Chained storage of queues
![[speech signal processing] 3 speech signal visualization -- prosody](/img/06/5f57f9dfe3a0f2f70022706f7d4d17.jpg)
[speech signal processing] 3 speech signal visualization -- prosody

栈-------

Chapter 14 signals (IV) - examples of multi process tasks

【脑洞大开】《西潮》及《走向世界丛书》

Mobile note application

Onenet Internet of things platform - create mqtts products and devices
![[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn](/img/f2/7931952b832e84d7b8f2615906f33f.png)
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
随机推荐
Machine learning - Data Science Library Day 3 - Notes
本科毕业四年:工作,辞职,结婚,买房
手把手教你完成图像分类实战——基于卷积神经网络的图像识别
Wechat applet reports an error: [rendering layer network layer error] pages/main/main Local resource pictures in wxss cannot be obtained through wxss. You can use network pictures, Base64, or < image/
What are the PHP FPM configuration parameters
List of QT players [easy to understand]
CPI tutorial - asynchronous interface creation and use
BIM and safety in road maintenance-buildSmart Spain
Understanding of NAND flash deblocking
Onenet Internet of things platform - the console sends commands to mqtt product devices
Like the three foot platform
下半年还有很多事要做
[shell programming] - shell introductory learning
R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
MySQL common functions
ASTM D 3801固体塑料垂直燃烧试验
栈-------
IOS interview
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns