当前位置:网站首页>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
边栏推荐
- C # WPF realizes the real-time screen capture function of screen capture box
- 统计php程序运行时间及设置PHP最长运行时间
- File creation, writing, reading, deletion (transfer) in go language
- Migration from go vendor project to mod project
- Excel快速合并多行数据
- 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
- Popular framework: the use of glide
- Rich text editing: wangeditor tutorial
- 为什么图片传输要使用base64编码
- [matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
猜你喜欢

Sqlserver functions, creation and use of stored procedures

(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!

The font of markdown grammar is marked in red

Detailed index of MySQL

Digi XBee 3 RF: 4个协议,3种封装,10个大功能

数据湖(十三):Spark与Iceberg整合DDL操作

Test process arrangement (3)

SqlServer函数,存储过程的创建和使用

电商系统中红包活动设计

How to package QT and share exe
随机推荐
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Fs4059c is a 5V input boost charging 12.6v1.2a. Inputting a small current to three lithium battery charging chips will not pull it dead. The temperature is 60 ° and 1000-1100ma is recommended
gin集成支付宝支付
Learn kernel 3: use GDB to track the kernel call chain
为什么图片传输要使用base64编码
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
软件测试之测试评估
Use of tiledlayout function in MATLAB
Leetcode T48:旋转图像
数据仓库面试问题准备
Leetcode T47: 全排列II
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
vscode 常用插件汇总
Vscode common plug-ins summary
Matters needing attention in overseas game Investment Agency
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
How to operate and invest games on behalf of others at sea
Popular framework: the use of glide
ViewModel 初体验