[LeetCode#256] 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, 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.

Analysis:

This kind of question is very easy and useful.
It actually represents a kind of dynamic programming problem.
The inheritence is that :
We don‘t know whether the current choice is optimal for next stage or not (the difference with greedy problem)? Thus, we delay the decision to make choice for current step at the next step.
Case:
Stage 1: [0 0] = 1; [0 1] = 5; [0 2] = 6
Stage 2: [1 0] = 1; [0 1] = 15; [0 2] = 17
At stage 1, if we use greedy algorithm, we definitely should choose the red color ([0 0] = 1). Actually this not a global optimal solution, since at stage 2, we have to choose from blue ([0 1] = 15) and green ([0 2] = 17).
The overall minimum cost for this solution is 16, which is much larger than the best plan: "[0 1] = 5" and "[1 0] = 1".

The problem with this solution is that, we could not make the stage 1‘s choice based on current information, since it would affect our available choices at stage 2. At current stage, we should only prepare the right information for next stage to directly use, and let the next stage to make choice for the current stage. 

Transitional function:
Assume:
min_red[i] : the minimum cost to paint houses from 0 to i, iff i was painted with red color.
min_blue[i] : the minimum cost to paint houses from 0 to i, iff i was painted with blue color.
min_green[i] : the minimum cost to paint houses from 0 to i, iff i was painted with green color. 

Transitional function.
min_red[i] = Math.min(min_blue[i-1], min_green[i-1]) + red_cost[i]
We actually made the decision for the previous stage at here. (if i house was painted as red). 

It‘s beautiful!!! Right??????

Solution:

public class Solution {
    public int minCost(int[][] costs) {
        if (costs == null)
            throw new IllegalArgumentException("costs is null");
        if (costs.length == 0)
            return 0;
        int min_red = costs[0][0];
        int min_blue = costs[0][1];
        int min_green = costs[0][2];
        int temp_red, temp_blue, temp_green;
        for (int i = 1; i < costs.length; i++) {
            temp_red = min_red;
            temp_blue = min_blue;
            temp_green = min_green;
            min_red = Math.min(temp_blue, temp_green) + costs[i][0];
            min_blue = Math.min(temp_red, temp_green) + costs[i][1];
            min_green = Math.min(temp_red, temp_blue) + costs[i][2];
        }
        return Math.min(Math.min(min_red, min_blue), min_green);
    }
}
时间: 2024-10-16 03:45:20

[LeetCode#256] Paint House的相关文章

[LeetCode] 256. Paint House_Easy tag: Dynamic Programming

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 sam

256.Paint House

/* * 256.Paint House * 2016-6-21 by Mingyang * 典型的dp,我一开始就想到了说要是用一个二维的dp,dp的另外一个值来表示颜色 * 这个地方最高级的部分就在于!我是在更新cost的matrix,并没有专门来建立一个dp array非常的高明! */ public int minCost(int[][] costs) { //请把最简单的case记住了 if(costs != null && costs.length == 0) return 0

[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

[LeetCode#276] Paint Fence

Problem: 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. N

[LeetCode] 276. 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. Note:n and

[LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming

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. Note:n and

[LC] 256. 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 sam

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

Leetcode 前 400 重点 250 题

这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者只有一两家出现进行删除.所以如在面试中出现删除题目概不负责,这只是从概率上删除低频,简单题目. 旨在减轻大家的刷