LeetCode#746 Min Cost Climbing Stairs

1、同#53、#121,都是不断取最好结果。
这里是:最后必定需要判断从i-1或i-2抵达的花费,因此层层递推,最后算到2和3的为为止。

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

        return cost.back();
    }
};

猜你喜欢

转载自blog.csdn.net/rpybd/article/details/82535079