当前位置:网站首页>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
边栏推荐
- Solutions to the problems of miui12.5 red rice k20pro using Au or povo2
- 2022游戏出海实用发行策略
- Abnormal value detection using shap value
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
- Error in find command: paths must precede expression (turn)
- Migration from go vendor project to mod project
- No servers available for service: xxxx
- 数据湖(十三):Spark与Iceberg整合DDL操作
- DDD application and practice of domestic hotel transactions -- Code
- Rich text editing: wangeditor tutorial
猜你喜欢
随机推荐
2022 game going to sea practical release strategy
gin集成支付宝支付
数据湖(十三):Spark与Iceberg整合DDL操作
vscode 常用插件汇总
R language dplyr package summary_ If function calculates the mean and median of all numerical data columns in dataframe data, and summarizes all numerical variables based on conditions
C# wpf 实现截屏框实时截屏功能
leetcode:6109. 知道秘密的人数【dp的定义】
R language ggplot2 visualization: gganimate package creates animated graph (GIF) and uses anim_ The save function saves the GIF visual animation
Visual Studio调试方式详解
Solutions aux problèmes d'utilisation de l'au ou du povo 2 dans le riz rouge k20pro MIUI 12.5
MySQL的存储过程练习题
Test process arrangement (2)
Install and use MAC redis, connect to remote server redis
R语言使用dplyr包的group_by函数和summarise函数基于分组变量计算目标变量的均值、标准差
Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
Some problems and ideas of data embedding point
MySQL的触发器
R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
Leetcode 61: rotating linked list





![Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)



