256. Paint House - cocoder39/coco39_LC GitHub Wiki

256. Paint House

there are 3 choices when painting house 1, namely red, blue, and green. if paint house 1 with red, house 0 can only be painted with blue or green. the cost is costs[1][0] + min(costs[0][1], cost[0][2]), which is the cost of painting house 1 with red costs[1][0]. When painting house 2, we don't need to care about house 0 any more.

int minCost(vector<vector<int>>& costs) {
        int sz = costs.size();
        if (sz == 0)    return 0;
        
        for (int i = 1; i < sz; i++) {
            costs[i][0] += min(costs[i - 1][1], costs[i - 1][2]);
            costs[i][1] += min(costs[i - 1][0], costs[i - 1][2]);
            costs[i][2] += min(costs[i - 1][0], costs[i - 1][1]);
        }
        
        return min(costs[sz - 1][0], min(costs[sz - 1][1], costs[sz - 1][2]));
    }

follow up is paint house ii

⚠️ **GitHub.com Fallback** ⚠️