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 ...
随机推荐
- 我与git“美妙”的一天
今天是第一天使用git,苦不堪言,感觉服务器和自己都要爆炸了,弄了半天才马马虎虎会了一点,基本流程如下 1.在mukever.online注册用户 2.下载git for windows(一个客户端) ...
- [转]JAVA 在main中访问内部类、方法等
1.使用静态的属性.方法.内部类 class A { static int i = 1; // A 类的静态属性 static void outPut() // A 类的静态方法 { System.o ...
- Final版本发布评论
1.飞天小女警组做的礼物挑选小工具,相比较beta版本并没有太大的改动,新增添了“猜你喜欢”模块,在最后给出的礼物推荐下面会给出三个猜你喜欢的礼物.这样解决了推荐礼物的单一,也让礼物推荐变得更有趣了. ...
- 在ubuntu下运行python脚本
转自http://www.cnblogs.com/hester/p/5575658.html 1. 运行方式一 新建test.py文件: 1 touch test.py 然后vim test.py打开 ...
- PAT 甲级 1021 Deepest Root
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...
- Xmind 8 pro 软件破解版
转载地址:https://blog.csdn.net/qq_16093323/article/details/80967867 Xmind是一款非常专业的思维导图软件,收费好几百元,不过还是很多用户, ...
- angular2 bootstrap modal
----html------- <div #ele class="modal fade " style="z-index: 9999" data-back ...
- angular浏览器滚动条滚动到指定element 触发事件
angular.module('app').directive('ScrollTrigger', () => { return { restrict: "A", link:f ...
- 从0在windows上一次性上传本地整个项目(包含所有文件/文件夹)到 Github
1.注册并登陆Github. 2.登陆进去之后的页面,点击这个“库”,这表示你在Github上上的代码仓库,我这里已经创建过一个了,所以数量是1 3.在仓库选项卡中,点击“新建”按钮添加一个项目. 4 ...
- [你必须知道的异步编程]C# 5.0 新特性——Async和Await使异步编程更简单
本专题概要: 引言 同步代码存在的问题 传统的异步编程改善程序的响应 C# 5.0 提供的async和await使异步编程更简单 async和await关键字剖析 小结 一.引言 在之前的C#基础知 ...