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色可选)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 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:二叉树下的不能相邻,求能 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. django admin 支持时间更改

    from django.db import models import django.utils.timezone as timezone class Doc(models.Model): add_d ...

  2. Docker修改本地镜像与容器的存储位置

    这个方法里将通过软连接来实现. 首先停掉Docker服务: systemctl restart docker或者service docker stop 然后移动整个/var/lib/docker目录到 ...

  3. css零碎知识点小结

    1.单行文字溢出显示省略号: div{ width: 200px; overflow: hidden; text-overflow:ellipsis; white-space: nowrap; } 2 ...

  4. DotNetBar创建的Ribbon、标签式多文档界面

    1.创建一个form作为主窗体,继承自:DevComponents.DotNetBar.RibbonForm 设置属性:IsMdiContainer为true 2.创建一个form,作为子窗体,也继承 ...

  5. 黄聪:xampp启动后mysql报Error

    2013-08-04 13:48:22 760 [ERROR] InnoDB: Attempted to open a previously opened tablespace. Previous t ...

  6. 双心一键获取winsxs的写入权限,解决VC运行库安装error1935错误

    @Echo offtitle 双心一键获取winsxs的写入权限,解决VC运行库安装error1935等错误set path=%path%;%~dp0setlocal EnableDelayedExp ...

  7. WINDOWS NT操作系统的注册表文件

    WINDOWS NT操作系统的注册表文件 WINDOWS NT注册表文件分为系统文件和用户文件两类. 系统设置和缺少用户 配置数据存放在系统C:\Windows\System32\config文件夹下 ...

  8. 【spring】之基于注解@ComponentScan的一些使用

    基于xml形式ComponentScan的使用如下 <context:component-scan base-package="com.luna" use-default-f ...

  9. keil5 MDK warning:registered ARM compiler version not found in path

    重装 打开keil5弹出窗口: warning:registered ARM compiler version not found in path... 解决: 增加系统环境变量 ARMCC5LIB ...

  10. css 实现多行文本末尾显示省略号

    思路: 省略号使用绝对定位添加,开头部分避免突兀使用c3渐变背景颜色 <!DOCTYPE html> <html lang="en"> <head&g ...