当前位置:网站首页>leetcode:329. The longest incremental path in the matrix [DFS + cache + no backtracking + elegance]
leetcode:329. The longest incremental path in the matrix [DFS + cache + no backtracking + elegance]
2022-07-01 12:36:00 【White speed Dragon King's review】
analysis
Why not visit, Because it is a strictly increasing sequence , So there must be no turning back , So there's no need to visit
Then the idea is the same , Traverse each starting point
dfs The maximum length is returned , The recorded parameters include the current location , And the position of the previous number
Because what we traverse is the starting point , So natural , The position of the previous number is the selected starting point
Then because if we fix the current position , And the size of the previous number , The maximum distance you can walk is also fixed
So it can be memorized
The core code here is , Leave the current problem to the next step ( Next, meet the conditions )
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 grace
# When the point is fixed with the previous point , The maximum length it can walk is fixed
@cache
def dfs(r, c, pre):
res = 1 # At least at this point
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
# Traverse each starting point to get the maximum value of different starting points
for i in range(m):
for j in range(n):
ans = max(ans, dfs(i, j, matrix[i][j]))
return ans
summary
Find the best path in two-dimensional graph
You can consider dfs + cache
dfs What is recorded , What are the parameters , What is the output
When can I enter the next dfs
the previous dfs And the next dfs What does it matter
need visit Prevent repetition
Need backtracking ...
边栏推荐
- 微信小程序 – 80个实用的微信小程序项目实例
- VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
- Chained storage of queues
- Operations related to sequence table
- 腾讯安全联合毕马威发布监管科技白皮书,解析“3+3”热点应用场景
- 晓看天色暮看云,美图欣赏
- Tencent security and KPMG released a regulatory technology white paper to analyze the "3+3" hot application scenarios
- [some notes]
- "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
- 下半年还有很多事要做
猜你喜欢
[some notes]
leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
队列操作---
被锡膏坑了一把
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
队列的链式存储
【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾
Stack-------
"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
BIM and safety in road maintenance-buildSmart Spain
随机推荐
Huawei interview question: Recruitment
Leetcode (Sword finger offer) - 58 - ii Rotate string left
Stack-------
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
VS Code 设置代码自动保存
ANSI/UL 94 VTM薄质材料垂直燃烧测试
fatal error: execution: 没有那个文件或目录
JS reverse | m3u8 data decryption of a spring and autumn network
CPI tutorial - asynchronous interface creation and use
[datawhale202206] pytorch recommendation system: multi task learning esmm & MMOE
[shell programming] - shell introductory learning
天青色等烟雨
2022-06-28-06-29
The difference between memcpy and strcpy
哪个券商公司开户佣金低又安全又可靠
localtime居然不可重入,踩坑了
[JS advanced] promise explanation
R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
Question d'entrevue de Huawei: recrutement
用.Net Core接入微信公众号开发