Paint House 解答
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 解答的更多相关文章
- 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 ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 优动漫PAINT基础系列之存储格式说明
本篇经验带大家了解优动漫PAINT可以存储成哪些格式! 最近有收到试用优动漫PAINT个人版试用版的小伙伴提问,优动漫PAINT可以导出什么格式文件呢?今天就这一问题做一下解答〜 优动漫PAINT[试 ...
- 详解Paint的setXfermode(Xfermode xfermode)
一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...
- android Canvas 和 Paint用法
自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...
- [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 ...
- [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 ...
- [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 ...
- 精选30道Java笔试题解答
转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...
随机推荐
- C++中构造函数初始化成员列表总结
1.只能在构造函数初始化列表初始化的成员变量的类型? a.const成员变量 b.引用类型的成员变量 c.static不能在初始化列表中进行初始化 d.类成员变量中有自定义类型的变量最好在初始化列表中 ...
- 代码高亮插件Codemirror使用方法及下载
代码高亮插件Codemirror使用方法及下载 - 老男孩的日志 - 网易博客 代码高亮插件Codemirror使用方法及下载 2013-10-31 16:51:29| 分类: 默认分类 | ...
- css中的段落样式及背景
一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...
- JS 事件冒泡整理 浏览器的事件流
JavaScript与HTML的交互通过事件来实现.而浏览器的事件流是一个非常重要的概念.不去讨论那些古老的浏览器有事件捕获与事件冒泡的争议, 只需要知道在DOM2中规定的事件流包括了三个部分,事件捕 ...
- ASE中的主要数据库
Adaptive Server包括多种类型数据库: 必需数据库. “附加功能”数据库 .例子数据库 .应用数据库 1.必需数据库 master 数据库包含系统表,这些系统表中存储的数据被用来管理,有 ...
- VS2010不能打开预编译的网站源码的原因是什么?(转之csdn)
原问题: 今天将写好的一个网站源码目录拷贝到另一台电脑上,但打开时提示: 你要打开一个预编译的网站,你可以查看该站点,但对它进行更改可能会造成该网站停止运行,若要修改站点,建议先编辑原始网站中的 ...
- 实施双工通信框架:SignalR
SignalR:基于Asp.net平台构建,利用JavaScript或者Websockets,实现在客户端与服务端异步通信的框架. Html5新规范:WebSocket
- Lucene文件扩展名
名称 文件后缀 描述 段文件(Segments File) segments.gen segments_N 存储提交点信息 锁文件(Lock File) write.lock 用来阻止多个indexW ...
- poj 3104 二分
Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12568 Accepted: 3243 Descripti ...
- C++Primer笔记(2)
大型程序一般都是分为多个模块,由多人协作来进行开发的,其中还不可避免的会用到库.而各个模块代码以及库中会定义大量变量,而大量变量的命名,不可避免的会遇见“重名”的问题.“重名”的情况我们称之为命名空间 ...