程式語言 - LeetCode - C++ - 746. Min Cost Climbing Stairs



參考資訊:
https://www.cnblogs.com/grandyang/p/8343874.html

題目:


解答:

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int max_step = cost.size();
        vector<int> dp(max_step, -1);

        auto dfs = [&](this auto&& dfs, int step) -> int {
            if (step >= max_step) {
                return 0;
            }

            if (dp[step] != -1) {
                return dp[step];
            }

            dp[step] = cost[step] + min(dfs(step + 1), dfs(step + 2));
            return dp[step];
        };

        return min(dfs(0), dfs(1));
    }
};