当前位置:网站首页>LeetCode:746. 使用最小花费爬楼梯【动态规划】
LeetCode:746. 使用最小花费爬楼梯【动态规划】
2022-08-02 03:11:00 【星空皓月】
题目描述
思路
经典动态规划题,要求到楼顶的最低花费,首先要求到达楼顶前的两层花费,要求这两层的花费,则需要知道这两层的前两层的花费…
假设dp[i]表示第i层的最低花费,需要知道第i-1层和第i-2层的消费那层更少,再加上第i层的消费。于是dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]。
AC代码
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
n = len(cost)
dp = [0] * n
dp[0], dp[1] = cost[0], cost[1]
for i in range(2, n):
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
return min(dp[-1], dp[-2])
边栏推荐
- (转帖)HashCode总结(2)
- 生成器知道鉴别器在无条件GANs中应该学习什么
- [LeetCode] 83. Delete duplicate elements in the sorted list
- # ODS及DWD层自动化构建##, 220731,
- WebShell连接工具(中国菜刀、WeBaCoo、Weevely)使用
- Heao Technology Network Interview (with reference answers)
- Double Strings (别总忘记substr)
- AntV X6制作画板工具(图形,线段,图片上传)
- JDBC的入门使用
- Common SQL interview questions: 50 classic examples
猜你喜欢
PHP WebSehll 后门脚本与检测工具
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
PyTorch(六)——PyTorch可视化
PHP WebSehll backdoor script and detection tool
PHP WebShell 免杀
第二章——堆栈、队列
JSP WebSehll 后门脚本
mysql8.0.28 download and installation detailed tutorial, suitable for win11
Reasons and solutions for Invalid bound statement (not found)
Hit the programmer interview scene: What did Baidu interviewers ask me?
随机推荐
Difference between #{} and ${}
7-42 整型关键字的散列映射 (25 分)
Lua安装及常用命令使用
给你一个大厂面试的机会,你能面试上吗?进来看看!
蓝鲸DevOps荣获平台类工具企业选择率第一
JSP WebSehll 后门脚本
mysql8.0.28下载和安装详细教程,适配win11
leetcode 143. 重排链表
关于跨域问题
7-43 字符串关键字的散列映射 (25 分) 谜之测试点
Go简单实现协程池
7-44 基于词频的文件相似度 (30 分)
浏览器的工作原理(dns域名服务器,tcp握手,ssl/tls安全协议,关键渲染路径,重绘及回流,防抖和节流)
STL入门基础 map和set容器
MySQL中根据日期进行范围查询
【LeetCode】206. Reverse linked list
5.nodejs--跨域、CORS、JSONP 、Proxy
DVWA之SQL注入
5.nodejs--cross domain, CORS, JSONP, Proxy
MySQL8.0.28 installation tutorial