当前位置:网站首页>leetcode:5270. 网格中的最小路径代价【简单层次dp】
leetcode:5270. 网格中的最小路径代价【简单层次dp】
2022-06-12 18:36:00 【白速龙王的回眸】

分析
从第一层开始
求初每一层的每个节点最小花费
比如说我要求这层的某个节点,就要看上层的值dp加上值再加上边权的最小值即可
注意我这里每层的dp不计入当前层的值
所以最后一层dp算完还要加上最后一层的值
要理解好题意
ac code
class Solution:
def minPathCost(self, grid: List[List[int]], moveCost: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
dp = [0] * n
for i in range(1, m):
ndp = [inf] * n
for j in range(n):
for k in range(n):
pre = grid[i - 1][k]
ndp[j] = min(ndp[j], dp[k] + pre + moveCost[pre][j])
dp = ndp
#print(dp)
for j in range(n):
dp[j] += grid[-1][j]
#print(dp)
return min(dp)
总结
每层贪心求最短
dp只与上一层有关系
边栏推荐
- Review of MySQL (I): go deep into MySQL
- 机器学习在美团配送系统的实践:用技术还原真实世界-笔记
- Gospel of audio and video developers, rapid integration of AI dubbing capability
- Random talk about redis source code 91
- [blockbuster release] ant dynamic card, enabling the app home page to realize agile update
- Enhanced version of unit test code displayed by SAP e-commerce cloud Spartacus UI checkout spinner
- MySQL - > > symbol usage JSON related
- PHP:Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocat
- Basic SQL statement - select (single table query)
- Gd32f4xx communicates with electric energy meter conforming to dlt645_ two
猜你喜欢

MYSQL:Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column

Machine learning series (3): logistic regression

Review of MySQL (I): go deep into MySQL

js两数之和

JS sum of two numbers

【sql语句基础】——查(select)(单表查询)

间隔两个月,我的第二次上榜纪念日【2022.6.2】

GD32F4xx控制DGUS 变量显示

Hash hash

【矩阵论 & 图论】期末考试复习思维导图
随机推荐
CVPR 2022 Oral 大连理工提出SCI:快速、超强的低光照图像增强方法
Review of MySQL (10): three paradigms of database
01 complexity
Gospel of audio and video developers, rapid integration of AI dubbing capability
In 2021, the global spice and seasoning revenue is about 18720million US dollars, and it is expected to reach 25960million US dollars in 2028
Review of MySQL (IX): index
Leetcode topic [string] - Sword pointing offer 05- replace spaces
Difference between rxjs of() and of ({})
Review of MySQL (VI): usage of Union and limit
What is SAP support package stack
Lenovo responded that there are too many and too messy notebooks: it is now the product adjustment period and will be divided into three series of digital /air/ pro in the future
基于Halcon的矩形卡片【手动绘制ROI】的自由测量
JS sum of two numbers
Machine learning series (3): logistic regression
Write a select based concurrent server
Title 54: take 4 ~ 7 bits of an integer a from the right end.
常用问题排查工具和分析神器,值得收藏
USB to serial port - serial port driver type
Leetcode topic [string] -151- flip words in string
基于Halcon的螺栓螺丝部分划痕、腐蚀缺陷检测