当前位置:网站首页>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
边栏推荐
- 使用CLion编译OGLPG-9th-Edition源码
- leetcode:6109. 知道秘密的人数【dp的定义】
- Nowcoder reverse linked list
- 游戏出海,全球化运营
- MySQL的触发器
- ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
- 统计php程序运行时间及设置PHP最长运行时间
- 实战解惑 | OpenCV中如何提取不规则ROI区域
- Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
- Progress in architecture
猜你喜欢
随机推荐
聊聊保证线程安全的 10 个小技巧
92.(cesium篇)cesium楼栋分层
nowcoder重排链表
AI与生命科学
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
Incremental ternary subsequence [greedy training]
使用CLion编译OGLPG-9th-Edition源码
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
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
Abnormal value detection using shap value
Learn kernel 3: use GDB to track the kernel call chain
富文本编辑:wangEditor使用教程
Leetcode T48: rotating images
Ml: introduction, principle, use method and detailed introduction of classic cases of snap value
卷积神经网络经典论文集合(深度学习分类篇)
产业互联网则具备更大的发展潜能,具备更多的行业场景
Test process arrangement (3)
第十七章 进程内存
Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering
The game goes to sea and operates globally







