[LintCode/LeetCode] Paint House I & Paint House II

463 查看

Paint House

Problem

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, costs0 is the cost of painting house 0 with color red; costs1 is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Example

Given costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

house 0 is blue, house 1 is green, house 2 is blue, 2 + 5 + 3 = 10

Note

在原数组上动规,每一行对应一个房子,每一个元素代表从第一行的房子到这一行的房子选择这一种颜色所花的最小开销。所以每个元素 = 该元素的值 + 上一行两个与该元素不同列元素的值的较小者
动规到

Solution

public class Solution {
    public int minCost(int[][] cost) {
        if (cost == null || cost.length == 0) return 0;
        int n = cost.length - 1;
        for (int i = 1; i <= n; i++) {
            cost[i][0] += Math.min(cost[i-1][1], cost[i-1][2]);
            cost[i][1] += Math.min(cost[i-1][0], cost[i-1][2]);
            cost[i][2] += Math.min(cost[i-1][0], cost[i-1][1]);
        }
        return Math.min(cost[n][0], Math.min(cost[n][1], cost[n][2]));
    }
}

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

Example

Given n = 3, k = 3, costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

house 0 is color 2, house 1 is color 3, house 2 is color 2, 2 + 5 + 3 = 10

Challenge

Could you solve it in O(nk)?

Note

和Paint House的思路一样,依然是在cost数组上更新最小花销之和。不过这次要记录三个变量:本行最小值,本行第二小值,本行最小值下标。这三个变量每行更新一次,同时也要存储上一次循环的三个变量。这样做的目的是当前一行最小值和之前一行最小值在同一列时,取之前一行的次小值,也就避免了相邻两行取同种颜色的问题。

Solution

public class Solution {
    public int minCostII(int[][] cost) {
        if (cost == null || cost.length == 0) return 0;
        int preMin = 0, preSec = 0, preIndex = -1;
        int n = cost.length, k = cost[0].length;
        for (int i = 0; i < n; i++) {
            int curMin = Integer.MAX_VALUE, curSec = Integer.MAX_VALUE, curIndex = 0;
            for (int j = 0; j < k; j++) {
                cost[i][j] += preIndex == j ? preSec : preMin;
                if (cost[i][j] < curMin) {
                    curSec = curMin;
                    curMin = cost[i][j];
                    curIndex = j;
                }
                else if (cost[i][j] < curSec) {
                    curSec = cost[i][j];
                }
            }
            preMin = curMin;
            preSec = curSec;
            preIndex = curIndex;
        }
        return preMin;
    }
}