当前位置:网站首页>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只与上一层有关系
边栏推荐
- 2022.6.12-----leetcode.890
- General differences between SQL server versions released by Microsoft in different periods so far, for reference
- 深圳3月14日起全市停工停业7天居家办公心得|社区征文
- Introduction to reinforcement learning and analysis of classic items 1.3
- 即时配送的订单分配策略:从建模和优化-笔记
- Analyzing mobx responsive refresh mechanism from source code
- When openharmony meets openeuler
- 232-CH579M学习开发-以太网例程-TCP服务器(项目应用封装,局域网或广域网测试)
- Gospel of audio and video developers, rapid integration of AI dubbing capability
- 快速复制浏览器F12中的请求到Postman/或者生成相关语言的对应代码
猜你喜欢

Go init initialization function

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

Introduction to service grid and istio - continued

Shenzhen has been shut down for 7 days since March 14. Home office experience | community essay solicitation

C language learning -- data storage in memory
![Free measurement of rectangular card [manual drawing ROI] Based on Halcon](/img/c5/d9109ed4024aff521e1788c63bff4e.png)
Free measurement of rectangular card [manual drawing ROI] Based on Halcon

Title 54: take 4 ~ 7 bits of an integer a from the right end.

在思科模拟器Cisco Packet Tracer实现自反ACL

Review of MySQL (VII): use of tables

What is SAP support package stack
随机推荐
C language operation database (SQLite3) call interface function
Implementing reflexive ACL in Cisco packet tracker
309. the best time to buy and sell stocks includes the freezing period
How to modify the authorization of sina Weibo for other applications
Two months later, my second listing anniversary [June 2, 2022]
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of swimming fins in the global market in 2022
Vue —— 进阶 vue-router 路由(二)(replace属性、编程式路由导航、缓存路由组件、路由的专属钩子)
基于Halcon的矩形卡片【手动绘制ROI】的自由测量
bilibili视频列表名字太长显示不全的解决方法
在思科模拟器Cisco Packet Tracer实现自反ACL
实验10 Bezier曲线生成-实验提高-交互式生成B样条曲线
Write a select based concurrent server
Adjust CEPH cluster image source
Gossip about the 88 of redis source code
Analyzing mobx responsive refresh mechanism from source code
A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
VirtualLab基础实验教程-5.泊松亮斑
Comparison of disk mapping tools for network disk and object cloud storage management
Go package import mode member visibility
MYSQL:Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column