当前位置:网站首页>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 ...
边栏推荐
- Huawei interview question: Recruitment
- 腾讯黎巍:深耕“监管科技”,护航数字经济行稳致远
- Ansible的playbook
- Switch basic experiment
- 微信小程序 – 80个实用的微信小程序项目实例
- Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
- [datawhale202206] pytorch recommendation system: precision model deepfm & DIN
- One year anniversary of bitbear live studio, hero rally order! I invite you to take a group photo!
- 单点登录SSO与JWT好文整理
- 关于NAND FLASH解扣的认识
猜你喜欢

BIM and safety in road maintenance-buildSmart Spain

数论基础及其代码实现
![[JS] interview questions](/img/f3/8cf430b999980190a250f89537715e.jpg)
[JS] interview questions
![[20211129] configuration du serveur distant du carnet de notes jupyter](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] configuration du serveur distant du carnet de notes jupyter

Mobile note application

Nc100 converts strings to integers (ATOI)

logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable

Sort out relevant contents of ansible

晓看天色暮看云,美图欣赏

Circular linked list--
随机推荐
Question d'entrevue de Huawei: recrutement
ROS2 Foxy depthai_ros教程
[JS] interview questions
【MAUI】为 Label、Image 等控件添加点击事件
R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
(mixed version 1) multiple TXT text to one table
用.Net Core接入微信公众号开发
ASTM D 3801 vertical burning test of solid plastics
6.30 simulation summary
STM32 project practice (1) introduction and use of photosensitive resistor
简单斐波那契(递推)
GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
【语音信号处理】3语音信号可视化——prosody
Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.
Operations related to sequence table
A hole in solder paste
[brain opening] west tide and going to the world series
MySQL的零拷贝技术
Wechat applet - 80 practical examples of wechat applet projects
Chapter 14 signals (IV) - examples of multi process tasks