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 0costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Example

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的更多相关文章

  1. 详解Paint的setXfermode(Xfermode xfermode)

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

  2. android Canvas 和 Paint用法

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

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. xp系统重绘边框线不显示(首次加载没有触发paint事件)

    同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...

  7. LeetCode Paint House II

    原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...

  8. LeetCode Paint House

    原题链接在这里:https://leetcode.com/problems/paint-house/ 题目: There are a row of n houses, each house can b ...

  9. zjuoj 3773 Paint the Grid

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3773 Paint the Grid Time Limit: 2 Secon ...

  10. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

随机推荐

  1. 云平台项目--学习经验--回调函数中call和apply

    Js中可以通过call和apply来代替另一个对象调用一个方法,将一个函数对象上下文从初始上下文改变为thisObj指定的新对象.简而言之,改变函数执行的上下文,而call和apply的基本区别在于他 ...

  2. 分析code

    1 using System; //跟系统说明一下可能会用到这个dll里面的东西 using System.Collections.Generic; //引用集合类命名空间 using System. ...

  3. kickstart自动化安装

    preboot execute environment预启动执行环境--intel开发的技术,计算机可以通过pxe协议从网络引导启动. 工作模式为客户端/服务器端的c/s模式 客户端从远端服务器下载镜 ...

  4. final评论2

    1-约跑APP 小组准备的非常的充分,还带了摄像头,整个发布过程清晰很多.可能是由于上次发布时没有完全展现出自己组的作品,所以这次发布做了充分的准备,本组重点放在了修改其他组发现的bug,团队的约跑项 ...

  5. 从苦逼到牛逼,详解Linux运维工程师的打怪升级之路

    做运维也快四年多了,就像游戏打怪升级,升级后知识体系和运维体系也相对变化挺大,学习了很多新的知识点. 运维工程师是从一个呆逼进化为苦逼再成长为牛逼的过程,前提在于你要能忍能干能拼,还要具有敏锐的嗅觉感 ...

  6. Vue实现对数组、对象的深拷贝、复制

    当组件间传递对象时,由于此对象的引用类型指向的都是一个地址(除了基本类型跟null,对象之间的赋值,只是将地址指向同一个,而不是真正意义上的拷贝),如下 数组: ,,]; var b = a; b.p ...

  7. Java进阶之路

    Java进阶之路——从初级程序员到架构师,从小工到专家. 怎样学习才能从一名Java初级程序员成长为一名合格的架构师,或者说一名合格的架构师应该有怎样的技术知识体系,这是不仅一个刚刚踏入职场的初级程序 ...

  8. libev学习之ev_run

    好吧,神马都init好了,loop毕竟是个环呐,在哪跑起来呢,ok,他是ev_run的工作: int ev_run (EV_P_ int flags) { #if EV_FEATURE_API ++l ...

  9. 架构师成长之路1.2-多功能系统信息统计工具dstat

    点击返回架构师成长之路 架构师成长之路1.2-多功能系统信息统计工具dstat dstat命令是一个用来替换vmstat.iostat.netstat.nfsstat和ifstat这些命令的工具,是一 ...

  10. bzoj5月月赛订正

    已完成2/9(要准备中考啊QwQ) T1 考虑对所有数分解质因数,其中因子>sqrt(100000)的因子最多有一个,于是我们可以暴力维护<sqrt(100000)的因子个数的前缀和. 剩 ...