Paint House
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 cost of painting each house with a certain color is represented by a n
x k
cost matrix. For example, costs[0][0]
is the cost of painting house 0
with color 0
; costs[1][2]
is the cost of painting house 1
with color 2
, and so on... Find the minimum cost to paint all houses.
Given n
= 3, k
= 3, costs
= [[14,2,11],[11,14,5],[14,3,10]]
return10
house 0 is color 2, house 1 is color 3, house 2 is color 2, 2 + 5 + 3 = 10
分析:
这题和在一个矩阵上冲一个点走到另一个点所需的cost是一样的。
从第二个房子开始,找出使用和上一家不同颜色能够获取的最小的cost。最后一排就可以算出总的价格。
public class Solution {
/**
* @param costs n x k cost matrix
* @return an integer, the minimum cost to paint all houses
*/
public int minCostII(int[][] costs) {
if (costs == null || costs.length == || costs[].length == ) return ; for (int i = ; i < costs.length; i++) {
for (int j = ; j < costs[].length; j++) {
int tempMin = Integer.MAX_VALUE;
for (int k = ; k < costs[].length; k++) {
if (k != j) {
if (tempMin > costs[i - ][k]) {
tempMin = costs[i - ][k];
}
}
}
costs[i][j] += tempMin;
}
}
int tempMin = Integer.MAX_VALUE;
for (int j = ; j < costs[].length; j++) {
if (costs[costs.length - ][j] < tempMin) {
tempMin = costs[costs.length - ][j];
}
}
return tempMin;
}
}
Paint House的更多相关文章
- 详解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 ...
- xp系统重绘边框线不显示(首次加载没有触发paint事件)
同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...
- LeetCode Paint House II
原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...
- LeetCode Paint House
原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...
- zjuoj 3773 Paint the Grid
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3773 Paint the Grid Time Limit: 2 Secon ...
- zjuoj 3780 Paint the Grid Again
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...
随机推荐
- php 中间件
PHP ::双冒号,意为静态成员的访问形式. 中间件$request 速查表:
- github建仓库注意
在导入新的项目工程时,github建仓库时不要选择readme文件初始化仓库
- CentOS7 如何修改 内核版本
1. 参考blog http://www.mamicode.com/info-detail-1758066.html https://www.cnblogs.com/sexiaoshuai/p/839 ...
- day3——关于<s:if/>和文件上传
一个小的注意点 <s:if test='#backyear==#yearatd'>selected="selected"</s:if> <s:if t ...
- 淘宝卖家搜索器V1.6算法注册机。
该软件的算法非常简单,适合小白练手.(E语言写的)1.OD加载该软件,输入bp MessageBoxA 2.点击注册 3.OD就会中断下来了 4.按ALT+K打开调用堆栈 5.往上翻,就来到算法处了 ...
- Tyvj1038 忠诚 (线段树)
[Tyvj1038]忠诚 线段树 题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意.但是 ...
- PL/SQL如何设置当前格局确保每次打开都给关闭前一样
打开plsql --> windows-->save layout 即可
- 【题解】 bzoj2748 [HAOI2012]音量调节 (动态规划)
懒得复制,戳我戳我 Solution: 傻逼题目,直接dp就可以了,他是求最后一次的最大值 Code: //It is coded by Ning_Mew on 4.17 #include<bi ...
- mes平台Action类模版
Connection conn = null; PreparedStatement pst = null; ResultSet rs = null; @Override public String e ...
- Nagios服务器端配置文件详解
Nagios服务器端安装部署详解见:http://www.cnblogs.com/ginvip/p/6505948.html Nagios 主要用于监控一台或者多台本地主机及远程的各种信息,包括本机资 ...