[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 ...
随机推荐
- 修改linux服务器的MySQL密码
1. 首先用管理员权限登陆Linux: 2. 输入:vi /etc/my.cnf 回车.然后按“i”键盘,在这个文件中的最后一行输入:skip-grant-tables 然后按 esc ...
- 浅谈JavaScript函数重载
上个星期四下午,接到了网易的视频面试(前端实习生第二轮技术面试).面了一个多小时,自我感觉面试得很糟糕的,因为问到的很多问题都很难,根本回答不上来.不过那天晚上,还是很惊喜的接到了HR面电话.现在HR ...
- java volatile
volatile可以保证变量的可见性 当一个变量定义为volatile后,此变量对所有的线程具有可见性.这里的可见性是指当一个线程修改了这个变量的值,新值对于其他线程来说是可以立即得知的. 每次使用v ...
- 实战 ant design pro 中的坑
1.替换mock数据: 1.将:.roadhogrc.mock.js 中的代理模式替换 当不使用代理的时候就会将所有 /api/的链接换成 http://localhost:8080/ export ...
- Azure VMSS (2) 对VM执行Generalize操作
<Windows Azure Platform 系列文章目录> 在本章中,笔者将介绍如何创建Azure Template镜像模板. 1.首先,我们先创建1台Windows Server 2 ...
- 【剑指offer】广度优先遍历二叉树
问题:从上往下打印出二叉树的每个节点,同层节点从左至右打印. *思路:先用队列存放树的根结点.每次出队一个结点,将结点非空的左右孩子分别入队.重复此过程,直到队列为空. import java.uti ...
- [转]检测到有潜在危险的 Request.Form 值
<system.web> <httpRuntime targetFramework="4.0" requestValidationMode="2.0&q ...
- Ajax提交from表单
一,使用Ajax提交form表单到后台传参问题 1,首先,定义一个form: <form class="form-horizontal" role="form&qu ...
- [ExcelHome]VLOOKUP的别样用法
请看题: 如上图所示,是某小区多名业主的信息表.如诸君所见,A列是业主的姓名,B列是一些有趣的信息,要求在C列,使用VLOOKUP函数,提取出B列的手机号码. B列的信息真是奇葩,除了手机号码,还有职 ...
- Permanent Space 和 Heap Space
JVM堆内存 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...