[leetcode]265. Paint House II粉刷房子(K色可选)
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.
Note:
All costs are positive integers.
Example:
Input: [[1,5,3],[2,9,4]]
Output: 5
Explanation: Paint house 0 into color 0, paint house 1 into color 2. Minimum cost: 1 + 4 = 5;
Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5.
题意:
一排有n套房子,每个房子可以选一种颜色(K种不同颜色)来粉刷。由于每个房子涂某种颜色的花费不同,求相邻房子不同色的粉刷最小花费。

做这个题的时候,想起跟老妈一起去过的Santa Cruz海棠花节的时候去过的这个网红海滩。
所以这个题,对于景区想打造网红般的colorful houses来讲,还是蛮有显示意义。
Solution1: DP




code
class Solution {
public int minCostII(int[][] costs) {
// sanity check
if (costs == null || costs.length == 0) return 0;
//init NO.1 house's curMin1, curMin2
int curMin1 = -1; // most minimum
int curMin2 = -1; // second minimum
for (int j = 0; j < costs[0].length; j++) {
if (curMin1 < 0 || costs[0][j] < costs[0][curMin1]) {
curMin2 = curMin1;
curMin1 = j;
} else if (curMin2 < 0 || costs[0][j] < costs[0][curMin2]) {
curMin2 = j;
}
}
// scan from NO.2 house
for (int i = 1; i < costs.length; i++) { // scan n houses
// get NO.1 house's curMin1, curMin2
int preMin1 = curMin1;
int preMin2 = curMin2;
// init NO.2 house's curMin1, curMin2
curMin1 = -1;
curMin2 = -1;
// try k colors
for (int j = 0; j < costs[0].length; j++) {
if (j != preMin1) {
costs[i][j] += costs[i - 1][preMin1];
} else {
costs[i][j] += costs[i - 1][preMin2];
}
// update NO.2 house's curMin1, curMin2
if (curMin1 < 0 || costs[i][j] < costs[i][curMin1]) {
curMin2 = curMin1;
curMin1 = j;
} else if (curMin2 < 0 || costs[i][j] < costs[i][curMin2]) {
curMin2 = j;
}
}
}
return costs[costs.length - 1][curMin1];
}
}
[leetcode]265. Paint House II粉刷房子(K色可选)的更多相关文章
- [LeetCode] 265. 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 II 粉刷房子之二
There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...
- [LintCode] 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#265] Paint House II
Problem: There are a row of n houses, each house can be painted with one of the k colors. The cost o ...
- [leetcode]256. Paint House粉刷房子(三色可选)
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- 265. Paint House II 房子涂色K种选择的版本
[抄题]: There are a row of n houses, each house can be painted with one of the k colors. The cost of p ...
- 265. Paint House II
题目: There are a row of n houses, each house can be painted with one of the k colors. The cost of pai ...
- LC 265. 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 ...
随机推荐
- String引用数据类型
一.String类的第一种方式 (原文地址:https://blog.csdn.net/wangdajiao/article/details/52087302)1.直接赋值 例:String str ...
- Hi3516EV100烧录出厂固件
1.Hitool烧录uboot 2.uboot下烧录固件 setenv serverip 192.168.1.138 mw.b ff ;tftp ;sf probe ;sf erase ;sf wri ...
- Zuul 跨域
JS访问会出现跨域问题的解决, 一.对单个接口,处理跨域,只需要在被调用的类或或方法增加注解 CoossOrigin 如下设置 allowCredenticals=true,表示运行Cookie跨域 ...
- 代码统计 (uustepcount)
代码统计软件(uustepcount)用于 记录自己的代码数量,包括空行,代码行数,注释行数,注释百分比,代码百分比,文件大小,文件日期等. 虽然也是 分析程序的源代码,统计空行,注释行,代码行,但u ...
- mongo数据的导入(mongoimport)和导出(mongoexport)
1.导出数据可以使用命令: mongoexport -h dbhost -d dbname -c collectionName -o output 参数说明: -h 数据库地址 -d 指明使用的库 - ...
- VS在.NETFramework升级时遇到类库冲突如何解决
相信大家在开发环境中随着程序的不断升级,很多时间需要升级. NETFramework版本.今天项目中遇到的问题是从. NETFramework4.0升级到4.5时提示 Entityframework. ...
- Python Pycharm 专题
http://www.themesmap.com/theme.html?t=time&page=3 一些好的主题地址 直接导入import settings就可以使用了
- centos7 安装percona-toolkit工具包的安装和使用
一.检查和安装与Perl相关的模块 PT工具是使用Perl语言编写和执行的,所以需要系统中有Perl环境. 依赖包检查命令为: rpm -qa perl-DBI perl-DBD-MySQL perl ...
- vue打包
npm installnpm run devnpm run build @types/node npm install --save @types/node npm install @types/no ...
- python自带的进程池及线程池
进程池 """ python自带的进程池 """ from multiprocessing import Pool from time im ...