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. Python完全新手教程

    转发:作者: taowen  来源: 博客园  发布时间: 2010-10-01 00:42  阅读: 1618 次  推荐: 0                  原文链接  [收藏] Lesson ...

  2. 第一天Python

    一.开发语言 高级语言:Python  Java.PHP     高级语言--字节码(PHP适用于写网页) 低级语言:C.汇编--机器码(底层开发,根本,效率低) 二.Python种类 三.安装

  3. tomcat的 tomcat-user.xml

    http://blog.csdn.net/asdeak/article/details/1879284 很多个tomcat因为在缺少 "  <role rolename="m ...

  4. Android开发 :androidstudio device offline

    使用设备调试的时候,偶尔的就会遇到androidstudio device offline,尽管尝试开启/关闭  USB调试 .都无济于事. 如果PC安装了360手机助手就好办了(我的360手机助手 ...

  5. ef中文文档

    https://entityframework.net/zh-CN/home 在使用ef进行对数据库操作时 数据库迁移 https://www.dotnettricks.com/learn/entit ...

  6. 源码:Java集合源码之:数组与链表(一)

    数组和链表是数据结构中最基本的部分. 数组 在java中,数组定义为一种基本类型,其可以通过下标获取到对应位置的数据.那么这种结构的数据,在内存中是怎么存放的呢? 数组在内存中是一段连续的存储单元,每 ...

  7. Mike Piehl

    some books were writen by him: new about this man website: http://paperstreetenterprises.com

  8. asp.net web api 权限验证的方法

    思路:客户端使用header或者form讲验证信息传入api,在权限验证过滤中进行处理,代码示例: 定义过滤器 public class ApiFilter1 : System.Web.Http.Au ...

  9. python网页爬虫开发之二

    1.网站robots robotparser模块首先加载robots.txt文件,然后通过can_fetch()函数确定指定的用户代理是否允许访问网页. 2.识别网站技术 3.下载网页 使用urlli ...

  10. Vue 双向数据绑定、事件介绍以及ref获取dom节点

    vue是一个MVVM的框架 M model V view MVVM  model改变会影响视图view,view改变会影响model 双向数据绑定必须在表单里面使用 //我发现在谷歌浏览器翻译后的网页 ...