当前位置:网站首页>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])
边栏推荐
猜你喜欢

7、MySQL Workbench 导出导入数据库

JunitTest单元测试

利用WebShell拿Shell技巧

R16 Type II量化反馈码本的产生

Chapter 10 聚类

Common SQL interview questions: 50 classic examples

Navicat cannot connect to database Mysql because of WiFi

(转帖)HashCode总结(2)

WebShell connection tools (Chinese kitchen knife, WeBaCoo, Weevely) use

iVX低代码平台系列详解 -- 概述篇(二)
随机推荐
合奥科技网络 面试(含参考答案)
Foundry教程:使用多种方式编写可升级的智能代理合约(下)
JSP Webshell free kill
关于#sql#的问题:该怎么写sql语句,
MySQL8 - use under Windows package installation method
7-42 整型关键字的散列映射 (25 分)
三维数字孪生引擎与实景互动,案例解析
JSP WebSehll backdoor script
mysql8.0.28 download and installation detailed tutorial, suitable for win11
Invalid bound statement (not found)出现的原因和解决方法
MySQL八股文背诵版
常见的SQL面试题:经典50例
Double Strings (别总忘记substr)
【LeetCode】20. Valid parentheses
Webshell upload method
5.nodejs--跨域、CORS、JSONP 、Proxy
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
STL入门基础 map和set容器
dropout
MySQL中的时间函数