当前位置:网站首页>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
边栏推荐
- Sqlserver functions, creation and use of stored procedures
- Leetcode T49: 字母异位词分组
- docker-compose公网部署redis哨兵模式
- Data center concept
- Leetcode t49: grouping of alphabetic words
- leetcode:6110. 网格图中递增路径的数目【dfs + cache】
- Xcode 异常图片导致ipa包增大问题
- No servers available for service: xxxx
- How to operate and invest games on behalf of others at sea
- Test process arrangement (3)
猜你喜欢
随机推荐
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
卷积神经网络经典论文集合(深度学习分类篇)
测试流程整理(3)
Leetcode 61: 旋转链表
DDD application and practice of domestic hotel transactions -- Code
Leetcode T48:旋转图像
Blob, text geometry or JSON column'xxx'can't have a default value query question
利用Shap值进行异常值检测
MySQL stored procedure exercise
统计php程序运行时间及设置PHP最长运行时间
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Rich text editing: wangeditor tutorial
leetcode:6109. 知道秘密的人数【dp的定义】
架构方面的进步
codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
数据湖(十三):Spark与Iceberg整合DDL操作
商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
Xcode 异常图片导致ipa包增大问题
NowCoder 反转链表
Data center concept