[Locked] Paint House I & II

Paint House

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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 a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

分析:

  典型动态规划,通过遍历所有情况可以弥补前面的选择对后面的影响。时间复杂度为O(n*3*3) = O(n);利用滚动数组,空间复杂度为O(3*2) = O(1)。

代码:

int minCost2(vector<vector<int> > cost) {
    vector<int> opt(3, 0), temp(3, 0);
    for(int i = 0; i < cost.size(); i++) {
        for(int j = 0; j < 3; j++) {
            temp[j] = INT_MAX;
            for(int k = 0; k < 3; k++) {
                if(j != k)
                    temp[j] = min(temp[j], opt[k] + cost[i][j]);
            }
        }
        opt.swap(temp);
    }
    int minc = INT_MAX;
    for(int i : opt)
        minc = min(minc ,i);
    return minc;
}

Paint House II

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 a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2]is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

Follow up:
Could you solve it in O(nk) runtime?

分析:

  如果采用I中的方法,时间复杂度为O(n*k*k),空间复杂度为O(k*2)。为了降低时间复杂度,可以通过减少两重k循环里的大量重复计算来使得O(k*k)的复杂度变为O(k)。此题中,对于j = j1, j2两种情况,它们的内部k循环有k-2次是重复比较了cost[i][j1] + x和cost[i][j2] + x的大小的,可以通过一次cost[i][j1]和cost[i][j2]的比较替代;扩展到j = 1...k种情况,只需找到小的cost[i][j], j = 1...k.

解法:

  动态规划,第i轮的最小代价是j = j1时,假设第i + 1轮中,j = j1是默认剔除的,很简单,前一轮的结果后一轮的最小结果都应该使用的,那么第i + 1轮的最小代价是min(cost[i+1][j])的j的取值时;然而j = j1并不是默认剔除的,故在第i + 1轮是cost[i + 1][j1]是无法使用上一轮的最小结果的,但它应该使用第二小的结果,故只需要比较第i + 1轮中,j == j1和j != j1两种情况的值即可。时间复杂度为O(n*(k + 常数)) = O(nk),空间复杂度,利用滚动值存储中间结果,为O(1)

代码:

int minCost(vector<vector<int> > cost) {
    int min1 = 0, min2 = 0, record = -1, c = INT_MAX;
    for(int i = 0; i < cost.size(); i++) {
        int minval1 = INT_MAX, minval2 = INT_MAX, last = -1;
        for(int j = 0; j < cost[0].size(); j++) {
            if(record != j) {
                if(minval1 > cost[i][j]) {
                    minval2 = minval1;
                    minval1 = cost[i][j];
                    last = j;
                }
                else
                    minval2 = min(minval2, cost[i][j]);
            }
        }
        int a = minval1 + min1, b = minval2 + min1;
        if(record != -1)
            c = cost[i][record] + min2;
        if(a < c) {
            record = last;
            min1 = a;
            min2 = min(b, c);
        }
        else {
            min1 = c;
            min2 = a;
        }

    }
    cout<<endl;
    return min1;
}
时间: 2024-10-25 15:29:37

[Locked] Paint House I & II的相关文章

[LeetCode] Paint House I &amp; II

Paint House There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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

[Locked] Meeting Room I &amp;&amp; II

Meeting Room Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. For example,Given [[0, 30],[5, 10],[15, 20]],return false. 分析: 即判断这些区间是否有重叠,遍历一遍

[Locked] Paint Fence

Paint Fence There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence

265. Paint House II

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 pai

*Paint House II

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 pai

[LeetCode#265] Paint House II

Problem: 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 co

Paint House II 解答

Question 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 co

[Locked] Read N Characters Given Read4 &amp; Read N Characters Given Read4 II - Call multiple times

Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. By using the rea

[LintCode] Paint House II 粉刷房子之二

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 pai