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公司笔试后靠记忆记下来的,经过整理献给与我 ...
随机推荐
- 符号表(Symbol Tables)
小时候我们都翻过词典,现在接触过电脑的人大多数都会用文字处理软件(例如微软的word,附带拼写检查).拼写检查本身也是一个词典,只不过容量比较小.现实生活中有许多词典的应用: 拼写检查 数据库管理应用 ...
- JAVA并发实现二(线程中止)
package com.subject01; public class InterruptDemo { public static void main(String[] args) { SimpleT ...
- banner背景通栏
background: #76CEF6 url("../images/bg.jpg") repeat-x 0 0; -webkit-background-size: 100%; ...
- android中使用哪种方式解析XML比較好
SAX是一个用于处理XML事件驱动的"推"模型. 长处是一种解析速度快而且占用内存少的xml解析器,它须要哪些数据再载入和解析哪些内容. 缺点是它不会记录标签的关系.而要让你的应用 ...
- C# IoC 容器
Unity是Unity是微软patterns& practices组用C#实现的轻量级,可扩展的依赖注入容器,它为方便开发者建立松散耦合的应用程序, 有以下优点: 1.简化了对象的创建,特别是 ...
- Fedora安装VirtualBox时出现错误Kernel driver not installed (rc=-1908)的解决办法
新建虚拟机后启动时出现如下错误: Kernel driver not installed (rc=-1908) The VirtualBox Linux kernel driver (vboxdrv) ...
- getAttribute()与getParameter的区别
当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getParamet ...
- React数据传递
React基础概念 React是基于组件化的开发,通过组件的组合,让web应用能够实现桌面应用的效果. React更有利于单页应用的开发. 并非MVC框架,只能算是V 具有单项数据流的特点 优势:代码 ...
- HTML与CSS入门——第八章 使用外部和内部链接
知识点: 1.链接锚的使用方法 2.在自己的网站上的页面之间链接的方法 3.链接到外部内容的方法 4.链接到一个E-mail地址的方法 5.在新浏览器窗口中查看链接的方法 6.用CSS为链接添加样式的 ...
- HTMl5的sessionStorage和localStorage(转)
html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...