LeetCode 746. Min Cost Climbing Stairs

递推公式: dp[i]=min(dp[i1]+cost[i],dp[i2]+cost[i]) d p [ i ] = m i n ( d p [ i − 1 ] + c o s t [ i ] , d p [ i − 2 ] + c o s t [ i ] )

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        vector<int> dp;
        dp.push_back(cost[0]);
        dp.push_back(cost[1]);
        int n = cost.size();
        for(int i=2;i1]+cost[i],dp[i-2]+cost[i]));
        }
        return min(dp[n-1],dp[n-2]);
    }
};

你可能感兴趣的:(LeetCode)