当前位置:网站首页>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防止重复吗
需要回溯吗。。。
边栏推荐
- Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
- 【20211129】Jupyter Notebook遠程服務器配置
- 6.30 simulation summary
- Application of stack -- bracket matching problem
- How to use opcache, an optimization acceleration component of PHP
- kubernetes之ingress探索实践
- Using burpsuite to capture app packages
- 关于NAND FLASH解扣的认识
- Exploration and practice of inress in kubernetes
- Ansible的playbook
猜你喜欢
![[brain opening] west tide and going to the world series](/img/b2/444af296e170d19629800b3d4c50fa.jpg)
[brain opening] west tide and going to the world series

Four years after graduation: work, resign, get married, buy a house

手机便签应用

顺序表有关操作
![[20211129] jupyter notebook remote server configuration](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] jupyter notebook remote server configuration

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

Typora realizes automatic uploading of picture pasting
![[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE](/img/8f/64fea641730795a2b5252cc2c8cdd2.png)
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE

STM32 project practice (1) introduction and use of photosensitive resistor

第十四章 信号(四)- 多进程任务示例
随机推荐
Exploration and practice of inress in kubernetes
系统测试UI测试总结与问题(面试)
The operation process of using sugar to make a large data visualization screen
Sleep quality today 79 points
第十四章 信号(四)- 多进程任务示例
Chained storage of queues
VS Code 设置代码自动保存
被锡膏坑了一把
Wechat applet - 80 practical examples of wechat applet projects
华为面试题: 招聘
[speech signal processing] 3 speech signal visualization -- prosody
華為面試題: 招聘
栈-------
"Analysis of 43 cases of MATLAB neural network": Chapter 40 research on prediction of dynamic neural network time series -- implementation of NARX based on MATLAB
Ansible的playbook
【20211129】Jupyter Notebook远程服务器配置
网络socket的状态要怎么统计?
硬阈值(Hard Thresholding)函数解读[通俗易懂]
Ansible Playbook
VS Code 设置单击打开新文件窗口,不覆盖前一个窗口