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);
}
}

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

  1. [leetcode]256. Paint House粉刷房子(三色可选)

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  2. [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 gr ...

  3. [LeetCode] 256. Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  4. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  5. [LeetCode] 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 paintin ...

  6. 【LeetCode】256. Paint House 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  7. 256. Paint House

    题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...

  8. [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 o ...

  9. [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 ...

随机推荐

  1. awk实用技巧

    awk '{sum+=$1} END {print "Sum = ", sum}' awk '{sum+=$1} END {print "Average = " ...

  2. php之常量小见

    php设置常量有二种方法,一为define(),二为使用关键字const. define()函数带有三个形参,一为常量名,通常以大写字母命名,二为值,三为是否对大小写敏感,其值为可选,默认为false ...

  3. React+Immutable.js的心路历程

    这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...

  4. php 计算代码行数

    <?php header("Content-type:text/html;charset=utf-8"); // php 递归计算文件夹代码行数 function codeL ...

  5. Oracel JDBC URL 和 Driver 的获取

    Driver 的获取 Driver Name:   oracle.jdbc.driver.OracleDriver Oracel JDBC URL的获取: URL:   jdbc:oracle:thi ...

  6. iOS,长按图片保存实现方法,轻松搞定!

    1.添加手势识别: UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@s ...

  7. CoreAnimation1-图层树、寄宿图以及图层几何学

    (一)图层的树状结构 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只 ...

  8. obj.onclick=fnClick与obj.onclick=fnClick()的区别

    先说结论:这段代码浏览器会报错,提示 aDiv[this.index] is undefined 所以正确的写法应该是去掉(),直接写为function fnClick.   不加括号的话,相当于指定 ...

  9. [LeetCode OJ] Word Search 深度优先搜索DFS

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  10. 【实习记】2014-08-15文档太少看着源码用cgicc+stl库之模板谓词函数对象

        总结1: 今天找到了昨天scanf的问题答案,scanf与printf一样的神奇而复杂,稍不留神,就会被坑.scanf函数在读入非空白符分割的多个字符串的解决方法是这个:/* 以 | 分割 * ...