当前位置:网站首页>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])
边栏推荐
猜你喜欢
随机推荐
I will give you a chance to interview in a big factory. Can you interview?Come in and see!
Navicat cannot connect to database Mysql because of WiFi
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
嵌入式分享合集25
5.nodejs--cross domain, CORS, JSONP, Proxy
PHP WebSehll backdoor script and detection tool
Brute force visitors
mysql使用on duplicate key update批量更新数据
5.nodejs--跨域、CORS、JSONP 、Proxy
Common SQL interview questions: 50 classic examples
EF Core:基于关系的复杂查询 区分IEnumerable和IQueryable
两对象数组比较拿出不同值方法
1. 获取数据-requests.get()
弹性盒子flex属性
CentOS7安装Oracle数据库的全流程
MySQL中的时区设置
5. Hezhou Air32F103_LCD_key
JSP Webshell 免杀
WebShell Feature Value Summary and Detection Tool
MySQL8--Windows下使用压缩包安装的方法