ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

265. Paint House II 房屋涂不同颜色的油漆

2021-11-01 03:00:30  阅读:228  来源: 互联网

标签:vector cost int house II Paint costs 265 color


There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

The cost of painting each house with a certain color is represented by an n x k cost matrix costs.

  • For example, costs[0][0] is the cost of painting house 0 with color 0costs[1][2] is the cost of painting house 1 with color 2, and so on...

Return the minimum cost to paint all houses.

 

Example 1:

Input: costs = [[1,5,3],[2,9,4]]
Output: 5
Explanation:
Paint house 0 into color 0, paint house 1 into color 2. Minimum cost: 1 + 4 = 5; 
Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5.

Example 2:

Input: costs = [[1,3],[2,4]]
Output: 5

参考:https://leetcode.com/problems/paint-house-ii/discuss/69502/Evolve-from-brute-force-to-optimal

This is similar to paint house.

 

  1. O((k-1)^n) brute force
    int minCostII(vector<vector<int>>& costs) {
        if(costs.empty()) return 0;
        return minCost(-1,-1,costs);        
    }
    int minCost(int i,int j, vector<vector<int>>& costs) { //minCost starting from house i with color j
        if(i==costs.size()) return 0;
        int mc = INT_MAX;
        for(int k=0;k<costs[0].size();k++) if(k!=j) mc = min(mc, minCost(i+1,k,costs));
        return i<0? mc : mc+costs[i][j];
    }
  1. O(nk^2) Memoization
    int minCostII(vector<vector<int>>& costs) {
        if(costs.empty()) return 0;
        vector<vector<int>> mem(costs.size(),vector<int>(costs[0].size()));
        return minCost(-1,-1,mem,costs);        
    }
    int minCost(int i,int j, vector<vector<int>>& mem, vector<vector<int>>& costs) {
        if(i==costs.size()) return 0;
        if(i>0 && mem[i][j]) return mem[i][j];
        int mc = INT_MAX;
        for(int k=0;k<costs[0].size();k++) if(k!=j) mc = min(mc, minCost(i+1,k,mem,costs));
        return i<0? mc : mem[i][j]=mc+costs[i][j];
    }
  1. O(nk^2) dp
    int minCostII(vector<vector<int>>& costs) {
        if(costs.empty()) return 0;
        int n = costs.size(), k = costs[0].size();
        vector<vector<int>> dp(n+1,vector<int>(k));
        for(int i=n-1;i>=0;i--)
            for(int j=0;j<k;j++)
                dp[i][j]=getMin(j,dp[i+1]) + costs[i][j];
        return getMin(-1, dp[0]);        
    }
    int getMin(int j, vector<int> &pre) {
        int mc = INT_MAX;
        for(int i=0;i<pre.size();i++) if(i!=j) mc = min(mc,pre[i]);
        return mc;
    }
  1. O(nk) dp
    int minCostII(vector<vector<int>>& costs) {
        int pre1=0,pre2=0,c1=-1;
        for(auto &v:costs) {
            int cur1=INT_MAX,cur2,co1;
            for(int i=0;i<v.size();i++) {
                int c = v[i]+ (i==c1?pre2:pre1);
                if(c<cur1) {
                    cur2 = cur1;
                    co1 = i;
                    cur1 = c;
                } else if (c<cur2) cur2 = c;
            }
            pre1 = cur1;
            pre2 = cur2;
            c1 = co1;
        }
        return pre1;
    }
 

标签:vector,cost,int,house,II,Paint,costs,265,color
来源: https://www.cnblogs.com/immiao0319/p/15491859.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有