当前位置:网站首页>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防止重复吗
需要回溯吗。。。
边栏推荐
- First intention is the most important
- 项目部署,一点也不难!
- Like the three foot platform
- List of QT players [easy to understand]
- Operations related to sequence table
- JS reverse | m3u8 data decryption of a spring and autumn network
- 题目 1004: 母牛的故事(递推)
- Typora adds watermarks to automatically uploaded pictures
- BIM and safety in road maintenance-buildSmart Spain
- 手把手教你完成图像分类实战——基于卷积神经网络的图像识别
猜你喜欢

Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
![[JS] interview questions](/img/f3/8cf430b999980190a250f89537715e.jpg)
[JS] interview questions

Onenet Internet of things platform - mqtt product equipment upload data points

codeforces -- 4B. Before an Exam

循环链表--

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

基因检测,如何帮助患者对抗疾病?

Typora adds watermarks to automatically uploaded pictures

本科毕业四年:工作,辞职,结婚,买房
![[20211129] jupyter notebook remote server configuration](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] jupyter notebook remote server configuration
随机推荐
Share several tools for designing exquisite circuit diagrams
Mysql database knowledge collation
Question d'entrevue de Huawei: recrutement
手机便签应用
MySQL的零拷贝技术
Circular linked list--
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
Onenet Internet of things platform - the console sends commands to mqtt product devices
木架的场景功能
R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
Onenet Internet of things platform - create mqtts products and devices
Chain storage of binary tree
Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
【MAUI】为 Label、Image 等控件添加点击事件
Relationship between accuracy factor (DOP) and covariance in GPS data (reference link)
【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾
First intention is the most important
Queue operation---
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
[20211129] jupyter notebook remote server configuration