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. 人民币-欧元预测(ARIMA算法)代码

    import pandas as pd import matplotlib.pyplot as plt import statsmodels as sm from statsmodels.graphi ...

  2. Linux命令常用

    数据库:查看日志 vim /var/log/mysqld.log

  3. 第八周PSP(11.5--11.9)

    2016.11.5 2016.11.6 2016.11.7 2016.11.8 2016.11.9

  4. Linux命令(十九) 查看系统负载 uptime

    一.命令介绍 Linux 系统中 uptime 命令主要用于获取主机运行时长和查询Linux系统负载等信息. uptime 命令可以显示系统已经运行了多长时间,信息显示依次为:现在时间.系统已经运行时 ...

  5. laravel 登录后跳转原来浏览的页面

    方法 1.修改一下文件/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RedirectsUsers.php 修改内容如下: 没有的加入 ...

  6. Android studio Gradle编译错误: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Reques

    两种处理方法: 1.修改distributionUrl链接 gradle-wrapper.properties文件 distributionUrl=https\://services.gradle.o ...

  7. 深入解读Quartz的原理

     Quartz是一个大名鼎鼎的Java版开源定时调度器,功能强悍,使用方便.   一.核心概念   Quartz的原理不是很复杂,只要搞明白几个概念,然后知道如何去启动和关闭一个调度程序即可.   1 ...

  8. 如何使用js判断视频是否可以成功访问

    日常工作中会出现各个公司网络不互通的情况,视频如果采用不互通的地址也无法播放,下面方法提供了检测视频是否可以播放的解决方案: 1.跨域 var video = document.createEleme ...

  9. VC++ 常见问题及其解决方法

    1. 无法找到“XXX.exe”的调试信息,或者调试信息不匹配: 选择 配置属性->链接器->调试->生成调试信息 改为 是 选择 配置属性->C/C++ ->常规-&g ...

  10. gitlab、openvpn配置ldap认证

    gitlab配置ldap认证: vim /etc/gitlab/gitlab.rb 添加以下内容: gitlab_rails['ldap_enabled'] = true gitlab_rails[' ...