当前位置:网站首页>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
边栏推荐
- No servers available for service: xxxx
- R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
- Leetcode t49: grouping of alphabetic words
- 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
- PyTorch的自动求导机制详细解析,PyTorch的核心魔法
- 如何游戏出海代运营、游戏出海代投
- Abnormal value detection using shap value
- R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
- Error in find command: paths must precede expression (turn)
- LifeCycle
猜你喜欢
codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
Data warehouse interview question preparation
数据中台概念
使用CLion编译OGLPG-9th-Edition源码
Digi重启XBee-Pro S2C生产,有些差别需要注意
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
C # WPF realizes the real-time screen capture function of screen capture box
Leetcode 61: rotating linked list
实战解惑 | OpenCV中如何提取不规则ROI区域
统计php程序运行时间及设置PHP最长运行时间
随机推荐
利用Shap值进行异常值检测
MySQL的触发器
sql优化之explain
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
【信息检索】链接分析
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Popular framework: the use of glide
關於miui12.5 紅米k20pro用au或者povo2出現問題的解决辦法
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
RK1126平台OSD的实现支持颜色半透明度多通道支持中文
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
基于51单片机的超声波测距仪
flink sql-client.sh 使用教程
Nowcoder reverse linked list
Test process arrangement (2)
Detailed index of MySQL
R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
opencv3.2 和opencv2.4安装
LifeCycle