256. Paint House

Medium (LinkedIn)

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.

public class Solution {

public int minCost(int[][] costs) {
    if (costs == null || costs.length == 0) {
        return 0;
    }

    int len = costs.length;
    int[][] dp = new int[len][3];

    dp[0][0] = costs[0][0];
    dp[0][1] = costs[0][1];
    dp[0][2] = costs[0][2];

    for (int i = 1; i < len; i++) {
        dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + costs[i][0];
        dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + costs[i][1];
        dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1]) + costs[i][2];
    }

    return Math.min(dp[len - 1][0], Math.min(dp[len - 1][1], dp[len - 1][2]));
}

}

Follow Up

Optimize the space complexity to O(1)

public class Solution {

public int minCost(int[][] costs) {
    if (costs == null || costs.length == 0) {
        return 0;
    }

    int preRedCost = costs[0][0];
    int preBlueCost = costs[0][1];
    int preGreenCost = costs[0][2];
    int curRedCost = costs[0][0];
    int curBlueCost = costs[0][1];
    int curGreenCost = costs[0][2];

    for (int i = 1; i < costs.length; i++) {
        curRedCost = Math.min(preBlueCost, preGreenCost) + costs[i][0];
        curBlueCost = Math.min(preRedCost, preGreenCost) + costs[i][1];
        curGreenCost = Math.min(preRedCost, preBlueCost) + costs[i][2];
        preRedCost = curRedCost;
        preBlueCost = curBlueCost;
        preGreenCost = curGreenCost;
    }

    return Math.min(curRedCost, Math.min(curBlueCost, curGreenCost));
}

}


265. Paint House II

Hard (Facebook)

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?

Solution 1:

Using priorityqueue and time complexity is O(nklogk), space complexity is O(nk); The time complexity will be improved to O(nk) by limit the size of priorityqueue to 2;

public class Solution {

  public int minCostII(int[][] costs) {
    if (costs == null || costs.length == 0 || costs[0].length == 0) {
        return 0;
    }

    int n = costs.length, k = costs[0].length;
    int[][] ans = new int[n][k];

    Queue<Node> maxHeap = new PriorityQueue<Node>(3, new Comparator<Node>(){
        public int compare(Node n1, Node n2) {
            return n2.cost - n1.cost;
        }
    });

    for(int i = 0; i < n; i++) {
        Node prevFirstMin = null;
        Node prevSecondMin = null;

        if(!maxHeap.isEmpty()) {
            prevSecondMin = maxHeap.poll();
            prevFirstMin = maxHeap.poll(); 
        }

        for(int j = 0; j < k; j++) {
            if(prevFirstMin == null) {
                maxHeap.offer(new Node(costs[i][j], j));
            } else if(j != prevFirstMin.index) {
                maxHeap.offer(new Node(costs[i][j] + prevFirstMin.cost, j));
            } else if(j == prevFirstMin.index) {
                maxHeap.offer(new Node(costs[i][j] + prevSecondMin.cost, j));
            }

            if(maxHeap.size() > 2) {
                maxHeap.poll();
            }
        }
    }

    if(maxHeap.size() >= 2) {  // test case [[8]]
        maxHeap.poll();
    }

    return maxHeap.poll().cost;
}

class Node {
    int cost;
    int index;

    public Node(int cost, int index) {
        this.cost = cost;
        this.index = index;
    }
}

}

Follow Up

Solution 2:

Time complexity is O(nk), space complexity is O(nk)

public class Solution {

public int minCostII(int[][] costs) {
    if (costs == null || costs.length == 0 || costs[0].length == 0) {
        return 0;
    }

    int curFirstMinIndex = -1, curSecondMinIndex = -1;
    int prevFirstMinIndex = -1, prevSecondMinIndex = -1;
    int n = costs.length, k = costs[0].length;
    int[][] ans = new int[n][k];

    for(int i = 0; i < n; i++) {
        prevFirstMinIndex = curFirstMinIndex;
        prevSecondMinIndex = curSecondMinIndex;
        curFirstMinIndex = curSecondMinIndex = -1;

        for(int j = 0; j < k; j++) {
            if(j != prevFirstMinIndex) {
                int curCost = prevFirstMinIndex == -1 ? 0 : ans[i - 1][prevFirstMinIndex];
                ans[i][j] = curCost + costs[i][j];
            } else {
                int curCost = prevSecondMinIndex == -1 ? 0 : ans[i - 1][prevSecondMinIndex];
                ans[i][j] = curCost + costs[i][j];
            }

            if(curFirstMinIndex == -1 || ans[i][curFirstMinIndex] > ans[i][j]) {
                curSecondMinIndex = curFirstMinIndex;
                curFirstMinIndex = j;
            } else if(curSecondMinIndex == -1 || ans[i][curSecondMinIndex] > ans[i][j]) {
                curSecondMinIndex = j;
            }
        }
    }

    return ans[n - 1][curFirstMinIndex];
}

}

Solution 3:

Time complexity is O(nk), space complexity is O(1)

public class Solution {

public int minCostII(int[][] costs) {
    if (costs == null || costs.length == 0) {
        return 0;
    }

    int curFirstMinIndex = -1, curSecondMinIndex = -1;
    int n = costs.length, k = costs[0].length;

    for (int i = 0; i < n; i++) {
        int preFirstMinIndex = curFirstMinIndex;
        int preSecondMinIndex = curSecondMinIndex;
        curFirstMinIndex = curSecondMinIndex = -1;

        for (int j = 0; j < k; j++) {
            if (j != preFirstMinIndex) {
                costs[i][j] += preFirstMinIndex < 0 ? 0 : costs[i - 1][preFirstMinIndex];
            } else {
                costs[i][j] += preSecondMinIndex < 0 ? 0 : costs[i - 1][preSecondMinIndex];
            }

            if (curFirstMinIndex < 0 || costs[i][curFirstMinIndex] > costs[i][j]) {
                curSecondMinIndex = curFirstMinIndex;
                curFirstMinIndex = j;
            } else if (curSecondMinIndex < 0 || costs[i][curSecondMinIndex] > costs[i][j]) {
                curSecondMinIndex = j;
            }
        }
    }

    return costs[n - 1][curFirstMinIndex];
}

}

results matching ""

    No results matching ""