当前位置:网站首页>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 ...
边栏推荐
- Share several tools for designing exquisite circuit diagrams
- CPI教程-异步接口创建及使用
- "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
- 工具箱之 IKVM.NET 项目新进展
- 用.Net Core接入微信公众号开发
- First intention is the most important
- ASP. Net core 6 from entry to enterprise level practical development application technology summary
- Double linked list related operations
- The difference between memcpy and strcpy
- 类的初始化与实例化
猜你喜欢

Mobile note application

Ipv6-6to4 experiment
![[speech signal processing] 3 speech signal visualization -- prosody](/img/06/5f57f9dfe3a0f2f70022706f7d4d17.jpg)
[speech signal processing] 3 speech signal visualization -- prosody
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

redis探索之缓存一致性

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

基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能

被锡膏坑了一把

Indefinite integral

leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】
随机推荐
79. 单词搜索【dfs + 回溯visit + 遍历起点】
ANSI/UL 94 VTM薄质材料垂直燃烧测试
codeforces -- 4B. Before an Exam
Double linked list related operations
ustime写出了bug
ASP. Net core 6 from entry to enterprise level practical development application technology summary
Nc100 converts strings to integers (ATOI)
【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾
Blocking sockets的读写操作该怎么玩?
The difference between memcpy and strcpy
Huawei interview question: Recruitment
ROS2 Foxy depthai_ros教程
[Yu Yue education] financial management reference materials of Ningbo University of Finance and Economics
Exploration and practice of inress in kubernetes
Ansible相关内容梳理
[106] 360 check font - check whether the copyright of local Fonts is commercially available
顺序表有关操作
双链表有关操作
[some notes]
[20211129] configuration du serveur distant du carnet de notes jupyter