使用最小花费爬楼梯

题源:leetcode

链接:https://leetcode-cn.com/problems/min-cost-climbing-stairs/

 

 

 一道经典的动态规划题

 1 class Solution {
 2 public:
 3     int minCostClimbingStairs(vector<int>& cost) {
 4         int n = cost.size();
 5         vector<int> dp(n + 1);
 6         dp[0] = dp[1] = 0;
 7         for (int i = 2; i <= n; i++) {
 8             dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
 9         }
10         return dp[n];
11     }
12 };

 

posted @ 2021-07-29 17:59  Danae丶  阅读(35)  评论(0编辑  收藏  举报