当前位置:网站首页>leetcode:6110. 网格图中递增路径的数目【dfs + cache】
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
2022-07-04 12:52:00 【白速龙王的回眸】
分析
dfs记忆化搜索,记录当前点的位置(至少有一条路径),然后把问题扔给下一个能走到的点,加上dfs
搜索每个位置为起点,后面能组成递增路径的总个数
遍历不同起点,由此得到总路径数
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优雅
# 当点和上一个点固定,它能走的最大长度也就固定
@cache
def dfs(r, c, pre):
res = 1 # 最少也是当前这个点
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
# 遍历每个起点获得不同起点的最大值
for i in range(m):
for j in range(n):
ans += dfs(i, j, grid[i][j])
ans %= MOD
return ans
总结
经典二维递增路径数量统计了
类似题目有最长的递增路径,只需要把res += 改成res = max(…)即可
边栏推荐
- Learn kernel 3: use GDB to track the kernel call chain
- Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
- Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
- 商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
- Data warehouse interview question preparation
- 数据埋点的一些问题和想法
- R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
- sql优化之explain
- Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
- 10.(地图数据篇)离线地形数据处理(供Cesium使用)
猜你喜欢
Leetcode T48:旋转图像
Oppo find N2 product form first exposure: supplement all short boards
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
Use of tiledlayout function in MATLAB
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
失败率高达80%,企业数字化转型路上有哪些挑战?
C# wpf 实现截屏框实时截屏功能
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Excel快速合并多行数据
随机推荐
IP lab monthly resumption · issue 5
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
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
第十七章 进程内存
Leetcode 61: 旋转链表
R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
92.(cesium篇)cesium楼栋分层
Migration from go vendor project to mod project
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
Oppo find N2 product form first exposure: supplement all short boards
Detailed index of MySQL
NowCoder 反转链表
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
Idea shortcut keys
富文本编辑:wangEditor使用教程
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
Gorm read / write separation (rotation)
实时数据仓库
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Excel快速合并多行数据