[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 ...
随机推荐
- windows server 修改远程桌面连接端口号
1. [运行]输入 regedit 2. 在注册表编辑器中找到以下PortNamber键,改为要使用的远程端口,如10000. HKEY_LOCAL_MACHINE\SYSTEM\CurrentCo ...
- c# 仿微信二维码生成
/// <summary> /// 生成二维码. /// </summary> /// <param name="data">需要添加进去的文本 ...
- C# 代码小技巧
一 .自动属性. 1.vs下输入prop,Tab键就出现了. 2.有了自动属性,我们不用再额外为一个类的每个公共属性定义一个私有字段(实际上没多大用处的字段), 但是通过反射还是可以看到对应的私有 ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- Percona XtraDB Cluster Strict Mode(PXC 5.7)
在Percona XtraDB Cluster集群架构中,为了避免多主节点导致的数据异常,或者说一些不被支持的特性引发的数据不一致的情形,PXC集群可以通过配置pxc_strict_mode这个变量来 ...
- delphi 字符串string转流TStream
function StringToFile(mString : string; mFileName : TFileName) : Boolean;var vFileChar : file of Cha ...
- JavaWeb——XML转义符字
被<![CDATA[]]>这个标记所包含的内容将表示为纯文本,比如<![CDATA[<]]>表示文本内容“<”. 此标记用于xml文档中,我们先来看看使用转义符的 ...
- 权限管理demo-Http请求前后监听工具
工具作用: 1. 输出每次请求的参数 2. 接口的请求时间 package com.mmall.common; import com.mmall.util.JsonMapper; import lom ...
- volatile适用场景之二
1.volatile最适用一个线程写,多个线程读的场合. 如果有多个线程并发写操作,仍然需要使用锁或者线程安全的容器或者原子变量来代替.(摘自Netty权威指南) 疑问:如果只是赋值的原子操作,是否可 ...
- centos7使用rpm方式安装mysql
https://blog.csdn.net/smiles13/article/details/81460617 (部分参考) 先查看是否安装mariadb rpm -qa | grep mari ...