C++ 6 lines DP


  • 0
    B

    nothing special

    class Solution {
    public:
        int minCost(vector<vector<int>>& costs) {
            vector<vector<int>>ans(costs.size()+1, vector<int>(3,0));
            for(int i = 0; i < costs.size(); i++)
            {
                ans[i+1][0] = costs[i][0] + min(ans[i][1], ans[i][2]);
                ans[i+1][1] = costs[i][1] + min(ans[i][0], ans[i][2]);
                ans[i+1][2] = costs[i][2] + min(ans[i][0], ans[i][1]);
            }
            return min(min(ans.back()[0], ans.back()[1]), ans.back()[2]);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.