Lintcode 516: Paint House II (DP好题)

516. 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 0costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Example

Example 1

Input:
costs = [[14,2,11],[11,14,5],[14,3,10]]
Output: 10
Explanation:
The three house use color [1,2,1] for each house. The total cost is 10.

Example 2

Input:
costs = [[5]]
Output: 5
Explanation:
There is only one color and one house.

Challenge

Could you solve it in O(nk)?

Notice

All costs are positive integers.

Input test data (one parameter per line)How to understand a testcase?

解法1:DP。第3层循环遍历colors,在跟当前color不同的color中找最小值。时间复杂度O(n^3)和空间复杂度O(n^2)都太高。

class Solution {
public:
    /**
     * @param costs: n x k cost matrix
     * @return: an integer, the minimum cost to paint all houses
     */
    int minCostII(vector<vector<int>> &costs) {
        int n = costs.size();
        if (n == 0) return 0;
        int colors = costs[0].size();
        
        //dp[i][j] is the minimum cost when ith house uses color j
        vector<vector<int>> dp(n, vector<int>(n, INT_MAX));
        
        for (int i = 0; i < colors; ++i) {
            dp[0][i] = costs[0][i];
        }
        
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j < colors; ++j) {
                for (int k = 0; k < colors; ++k) {
                    if (k != j) dp[i][j] = min(dp[i][j], dp[i - 1][k] + costs[i][j]);
                }
            }
        }
        
        int result = INT_MAX;
        for (int i = 0; i < colors; ++i) {
            result = min(result, dp[n - 1][i]);
        }
        
        return result;
    }
};

解法2:改进版。只需保存前次节点的最小值和第二小的值和其对应的colors即可,如果当前color和前次节点的最小值对应的color一样,则取前次节点的第二小的值,否则取前次节点的最小值。

时间复杂度O(n^2),空间复杂度还是O(n^2)。
 

class Solution {
public:
    /**
     * @param costs: n x k cost matrix
     * @return: an integer, the minimum cost to paint all houses
     */
    int minCostII(vector<vector<int>> &costs) {
        int n = costs.size();
        if (n == 0) return 0;
        int colors = costs[0].size();
        if (colors <= 1) return 0;
        
        //dp[i][j] is the minimum cost when ith house uses color j
        vector<vector<int>> dp(n, vector<int>(n, INT_MAX));
        int min1 = 0, min2 = 1;
        
        if (costs[0][0] > costs[0][1]) {
            min1 = 1;
            min2 = 0;
        }
        dp[0][0] = costs[0][0];
        dp[0][1] = costs[0][1];
        for (int i = 2; i < colors; ++i) {
            dp[0][i] = costs[0][i];
            if (dp[0][i] < dp[0][min1]) {
                min2 = min1;
                min1 = i;
            } else if (dp[0][i] < dp[0][min2]) {
                min2 = i;
            }
        }

        for (int i = 1; i < n; ++i) {
            int last1 = min1, last2 = min2;
            min1 = -1; min2 = -1;
            for (int j = 0; j < colors; ++j) {
                if (j == last1) {
                    dp[i][j] = dp[i - 1][last2] + costs[i][j];
                } else {
                    dp[i][j] = dp[i - 1][last1] + costs[i][j];
                }
                if (min1 < 0 || dp[i][j] < dp[i][min1]) {
                    min2 = min1;
                    min1 = j;
                } else if (min2 < 0 || dp[i][j] < dp[i][min2]) {
                    min2 = j;
                }
            }
        }

        
        return dp[n - 1][min1];
    }
};

解法3:类似解法2,但不用二维数组了,因为只需要保存前次节点的4个值即可(min1_id, min2_id, min1_v, min2_v)。

时间复杂度O(n^2),空间复杂度O(1)。

class Solution {
public:
    /**
     * @param costs: n x k cost matrix
     * @return: an integer, the minimum cost to paint all houses
     */
    int minCostII(vector<vector<int>> &costs) {
        int n = costs.size();
        if (n == 0) return 0;
        int colors = costs[0].size();
        if (colors <= 1) return 0;

        int min1_id = 0, min1_v = costs[0][0];
        int min2_id = 1, min2_v = costs[0][1];
        
        if (costs[0][0] > costs[0][1]) {
            min1_id = 1; min1_v = costs[0][1];
            min2_id = 0; min2_v = costs[0][0];
        }

        for (int i = 2; i < colors; ++i) {
            if (costs[0][i] < costs[0][min1_id]) {
                min2_id = min1_id; min2_v = min1_v;
                min1_id = i; min1_v = costs[0][i];
            } else if (costs[0][i] < costs[0][min2_id]) {
                min2_id = i; min2_v = costs[0][i];
            }
        }

        for (int i = 1; i < n; ++i) {
            int last1_id = min1_id, last1_v = min1_v;
            int last2_id = min2_id, last2_v = min2_v;
            min1_id = -1; min2_id = -1;
            for (int j = 0; j < colors; ++j) {
                int curr_v;
                if (j == last1_id) {
                    curr_v = last2_v + costs[i][j];
                } else {
                    curr_v = last1_v + costs[i][j];
                }
                if (min1_id < 0 || curr_v < min1_v) {
                    min2_id = min1_id; min2_v = min1_v;
                    min1_id = j; min1_v = curr_v;
                } else if (min2_id < 0 || curr_v < min2_v) {
                    min2_id = j;
                    min2_v = curr_v;
                }
            }
        }

        return min1_v;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值