当前位置:网站首页>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 vertical burning test for thin materials
- 硬阈值(Hard Thresholding)函数解读[通俗易懂]
- 【datawhale202206】pyTorch推荐系统:多任务学习 ESMM&MMOE
- Eurake分区理解
- QT 播放器之列表[通俗易懂]
- Ansible Playbook
- Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
- 简单斐波那契(递推)
- "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
- Blue Bridge Cup multi interface switching processing (enumeration plus state machine method)
猜你喜欢
![[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 8](/img/16/e1a0a52964c8a55eb729469114fc60.jpg)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 8

A hole in solder paste

顺序表有关操作

队列的链式存储

VS Code 设置单击打开新文件窗口,不覆盖前一个窗口

《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现
![[20220605] Literature Translation -- visualization in virtual reality: a systematic review](/img/11/6c42957186bf530e8f9d4025a40197.png)
[20220605] Literature Translation -- visualization in virtual reality: a systematic review

使用nvm管理nodejs(把高版本降级为低版本)
![[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE](/img/8f/64fea641730795a2b5252cc2c8cdd2.png)
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE

被锡膏坑了一把
随机推荐
[JS advanced] promise explanation
MySQL workbench data modeling function
数论基础及其代码实现
Nc100 converts strings to integers (ATOI)
Mobile note application
Indefinite integral
VS Code 设置代码自动保存
Like the three foot platform
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
硬阈值(Hard Thresholding)函数解读[通俗易懂]
Interpretation of hard threshold function [easy to understand]
localtime居然不可重入,踩坑了
Ansible Playbook
Digital signal processing -- Design of linear phase (Ⅱ, Ⅳ) FIR filter (2)
Question d'entrevue de Huawei: recrutement
Exploration and practice of inress in kubernetes
Chapter 14 signals (IV) - examples of multi process tasks
Zero copy technology of MySQL
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
Chained storage of queues