[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 ... 
随机推荐
- 第五节《Git基本操作》
			我们给原来的数据打一个tag(标签),专业术语叫做“里程碑”,我们先不介绍里程碑的奥秘,只要知道里程碑无非也是一个引用而已. [root@git demo]# pwd/git/my/workspace ... 
- zombodb 数据类型映射
			zombodb 与es 数据类型的映射处理 通用数据类型映射 Postgres 类型 Elasticsearch JSON 映射定义 bytea {"type": "bi ... 
- bzoj 1814 Ural 1519 Formula 1 ——插头DP
			题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ... 
- 【转】Entity Framework 复杂类型
			为了说明什么是复杂属性,先举一个例子. public class CompanyAddress { public int ID { get; set; } public string CompanyN ... 
- 查看mysql的版本号
			查看mysql的版本号 1.1 在命令行登录mysql,即可看到mysql的版本号 [root@heyong ~]# mysql -uroot -p Enter password: Welcome t ... 
- Azure VMSS (1) 入门
			<Windows Azure Platform 系列文章目录> 在使用云计算服务的时候,我们经常需要有自动横向扩展的功能.比如: 1.在业务高峰期,根据负载的增加,自动打开若干台VM 2. ... 
- linux命令行命令
			Linux命令行编辑快捷键: history 显示命令历史列表 ↑(Ctrl+p) 显示上一条命令 ↓(Ctrl+n) 显示下一条命令 !num 执行命令历史列表的第num条命令 !! 执行上一条命令 ... 
- 给idea添加类注释和方法注释模板
			这是我找到的最好的,最简单明白的一文: https://blog.csdn.net/xiaoliulang0324/article/details/79030752 
- bzoj5048: 塌陷的牧场
			Description 农夫小Q将他的奶牛们饲养在一个长n宽m的矩形网格牧场中.行从上到下依次编号为1到n,列从左往右依次编号为1 到m.为了防止奶牛们逃跑,小Q在牧场外圈安装了一排电网,只要奶牛走出 ... 
- PP.io的三个阶段,“强中心”——“弱中心”——“去中心”
			什么是PP.io? PP.io是我和Bill发起的存储项目,目的在于为开发者提供一个去中心化的存储和分发平台,能做到更便宜,更高速,更隐私. 当然做去中心化存储的项目也有好几个,FileCoin,Si ... 
