当前位置:网站首页>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])
边栏推荐
猜你喜欢
随机推荐
MySQL函数(经典收藏)
mysql8.0.28 download and installation detailed tutorial, suitable for win11
just write blindly = feelings
Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers
rem适配
MySQL8 -- use msi (graphical user interface) under Windows installation method
MySQL8.0.28安装教程
Keil开发环境安装教程
Double Strings (别总忘记substr)
总体写作原则
合奥科技网络 面试(含参考答案)
基于优化的多核局部费舍尔判别分析的故障分类
7-44 基于词频的文件相似度 (30 分)
2W字!梳理50道经典计算机网络面试题(收藏版)
【LeetCode】145. Postorder Traversal of Binary Tree
8万字带你入门Rust
5.nodejs--cross domain, CORS, JSONP, Proxy
PHP WebShell 免杀
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
MySQL8--Windows下使用压缩包安装的方法








