当前位置:网站首页>leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
leetcode:6110. The number of incremental paths in the grid graph [DFS + cache]
2022-07-04 14:27:00 【White speed Dragon King's review】
analysis
dfs Memory search , Record the position of the current point ( At least one path ), Then throw the problem to the next accessible point , add dfs
Search each location as the starting point , The total number of incremental paths that can be formed later
Traverse different starting points , From this, we get the total number of paths
ac code
class Solution:
def countPaths(self, grid: List[List[int]]) -> int:
MOD = 10 ** 9 + 7
m, n = len(grid), len(grid[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 grid[nr][nc] > pre:
res += dfs(nr, nc, grid[nr][nc])
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 += dfs(i, j, grid[i][j])
ans %= MOD
return ans
summary
The number of classic two-dimensional incremental paths is counted
Similar topics have the longest incremental path , Only need to res += Change to res = max(…) that will do
边栏推荐
- Leetcode T49: 字母异位词分组
- Xcode 异常图片导致ipa包增大问题
- R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
- 關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
- Excel quickly merges multiple rows of data
- 【云原生】我怎么会和这个数据库杠上了?
- 统计php程序运行时间及设置PHP最长运行时间
- redis 日常笔记
- vscode 常用插件汇总
- Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
猜你喜欢
Talk about 10 tips to ensure thread safety
Vscode common plug-ins summary
The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
sql优化之explain
为什么图片传输要使用base64编码
Use of tiledlayout function in MATLAB
Excel快速合并多行数据
Rich text editing: wangeditor tutorial
C# wpf 实现截屏框实时截屏功能
测试流程整理(3)
随机推荐
测试流程整理(3)
ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
AI and Life Sciences
Migration from go vendor project to mod project
产业互联网则具备更大的发展潜能,具备更多的行业场景
RK1126平台OSD的实现支持颜色半透明度多通道支持中文
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Leetcode T47: 全排列II
R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
聊聊保证线程安全的 10 个小技巧
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
Use of tiledlayout function in MATLAB
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
STM32F1与STM32CubeIDE编程实例-MAX7219驱动8位7段数码管(基于GPIO)
Gorm data insertion (transfer)
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions