当前位置:网站首页>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
边栏推荐
- Leetcode 61: 旋转链表
- R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
- R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
- AI与生命科学
- 【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
- 2022 game going to sea practical release strategy
- 【信息检索】分类和聚类的实验
- No servers available for service: xxxx
- Redis daily notes
- 测试流程整理(2)
猜你喜欢

Compile oglpg-9th-edition source code with clion

flink sql-client.sh 使用教程

ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例

92.(cesium篇)cesium楼栋分层

Leetcode 61: rotating linked list

PyTorch的自动求导机制详细解析,PyTorch的核心魔法

使用CLion编译OGLPG-9th-Edition源码
![去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]

Rich text editing: wangeditor tutorial

scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
随机推荐
Digi XBee 3 RF: 4个协议,3种封装,10个大功能
实战解惑 | OpenCV中如何提取不规则ROI区域
File creation, writing, reading, deletion (transfer) in go language
Excel quickly merges multiple rows of data
GCC【6】- 编译的4个阶段
ARouter的使用
R language ggplot2 visualization: gganimate package creates animated graph (GIF) and uses anim_ The save function saves the GIF visual animation
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
基于51单片机的超声波测距仪
10.(地图数据篇)离线地形数据处理(供Cesium使用)
DDD application and practice of domestic hotel transactions -- Code
产业互联网则具备更大的发展潜能,具备更多的行业场景
Common content type correspondence table
数据中台概念
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
按照功能对Boost库进行分类
R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
sql优化之explain
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]