Question

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.

Solution

拿到这题的第一反应是画出解空间树。我们用1, 2, 3分别代表red, green, blue

      ()

     /  |  \

    1    2    3

/ \   / \   / \

   2  3 1  3 1  2

   ...................

粗暴的方法是用DFS遍历整个解空间树。但是我们可以看到在每一层,其实有重复计算。

所以这题的思路和那道经典的求min path sum一样,是用DP。Time complexity O(n), space cost O(1)

cost1, cost2, cost3表示第n层选了1/2/3后的最少话费。

举个例子:

    red  green  blue

h1   1    2    3

h2   3    1    2

h3   4    3    2

我们从底向上遍历做DP

对于h3这一层:

cost1 = 4, cost2 = 3, cost3 = 2

对于h2这一层:

cost1' = 3 + min(cost2, cost3) = 5, cost2' = 1 + min(cost1, cost3) = 3, cost3' = 2 + min(cost1, cost2) = 5

对于h1这一层:

cost1'' = 1 + min(cost2', cost3') = 4, cost2'' = 2 + min(cost1', cost3') = 7, cost3'' = 3 + min(cost1', cost2') = 6

因此最少话费是cost1''

 public class Solution {
public int minCost(int[][] costs) {
if (costs == null || costs.length < 1) {
return 0;
}
int m = costs.length, n = costs[0].length;
int cost1 = costs[m - 1][0], cost2 = costs[m - 1][1], cost3 = costs[m - 1][2];
for (int i = m - 2; i >= 0; i--) {
int tmp1 = cost1, tmp2 = cost2, tmp3 = cost3;
cost1 = costs[i][0] + Math.min(tmp2, tmp3);
cost2 = costs[i][1] + Math.min(tmp1, tmp3);
cost3 = costs[i][2] + Math.min(tmp1, tmp2);
}
int result = Math.min(cost1, cost2);
return Math.min(result, cost3);
}
}

Paint House 解答的更多相关文章

  1. Paint House II 解答

    Question There are a row of n houses, each house can be painted with one of the k colors. The cost o ...

  2. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  3. 优动漫PAINT基础系列之存储格式说明

    本篇经验带大家了解优动漫PAINT可以存储成哪些格式! 最近有收到试用优动漫PAINT个人版试用版的小伙伴提问,优动漫PAINT可以导出什么格式文件呢?今天就这一问题做一下解答〜 优动漫PAINT[试 ...

  4. 详解Paint的setXfermode(Xfermode xfermode)

    一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...

  5. android Canvas 和 Paint用法

    自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...

  6. [LeetCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

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

  8. [LeetCode] Paint House 粉刷房子

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

  9. 精选30道Java笔试题解答

    转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...

随机推荐

  1. Saruman's Army (POJ 3069)

    直线上有N个点.点i的位置是Xi.从这N个点中选择若干个,给它们加上标记.对每一个点,其距离为R以内的区域里必须又带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点).在 ...

  2. 关于ognl.OgnlException: target is null for setProperty(null的解决方案

    在跑struts2的时候有时候会出现上面的错,特别是新手, 这种情况是在struts2高级的POJO访问时候出现的s 警告: Error setting expression 'user.passwo ...

  3. poj 2456 Aggressive cows(二分搜索之最大化最小值)

    Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...

  4. python3-day4(装饰器)

    一.基本 第一波 #### def foo():     print 'foo'   foo     #表示是函数 foo()   #表示执行foo函数   #### 第二波 #### def foo ...

  5. Java中普通代码块,构造代码块,静态代码块的代码演示样例及区分

    //运行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 当中静态代码块仅仅运行一次.构造代码块在每次创建对象是都会运行. 1 普通代码块 <span ...

  6. 开源 免费 java CMS - FreeCMS1.5-系统配置

    下载地址:http://code.google.com/p/freecms/ 系统配置 从FreeCMS 1.2 开始支持 管理系统使用的配置项. 从左侧管理菜单点击系统配置进入. 从FreeCMS ...

  7. Codeforces 468B Two Sets 并查集

    题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...

  8. Layer中自定义属性的动画

    转载自:http://blog.jobbole.com/69211/ 默认情况下,CALayer 及其子类的绝大部分标准属性都可以执行动画,无论是添加一个 CAAnimation 到 Layer(显式 ...

  9. javascript监听事件兼容

    function addEvent(el ,type ,fn){ if(el.addEventListener){ el.addEventListener(type,fn,false); }else ...

  10. SQL数据库注入防范 ASP.NET Globle警告

    在项目中的Global.asax页面代码中加下面的代码,就可以有效的防范简单的SQL注入. protected void Application_BeginRequest(Object sender, ...