题目:

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. 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 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red;costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

链接: http://leetcode.com/problems/paint-house/

题解:

房子用RGB刷漆, 每种漆对于每栋房子来说花费不一样,要求相邻两房子不一个色,并且花费最小。 这道题看提示需要用dp做。一开始我想设一个sum,一个last color = -1,不过没办法解决duplicate的问题。所以还是参考了jianchao.li大神的代码,对RGB分别进行dp。

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public int minCost(int[][] costs) { //dp
if(costs == null || costs.length == 0) {
return 0;
}
int len = costs.length, red = 0, blue = 0, green = 0;
for(int i = 0; i < costs.length; i++) {
int prevRed = red, prevBlue = blue, prevGreen = green;
red = costs[i][0] + Math.min(prevBlue, prevGreen);
blue = costs[i][1] + Math.min(prevRed, prevGreen);
green = costs[i][2] + Math.min(prevRed, prevBlue);
} return Math.min(red, Math.min(blue, green));
}
}

二刷:

继续锻炼思维能力,现在写这类题目已经比较轻松了,看来自己真的是在进步的。

Java:

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public int minCost(int[][] costs) {
if (costs == null || costs.length == 0) return 0;
int pRed = 0, pGreen = 0, pBlue = 0;
for (int[] cost : costs) {
int lastRed = pRed, lastGreen = pGreen, lastBlue = pBlue;
pRed = cost[0] + Math.min(lastGreen, lastBlue);
pGreen = cost[1] + Math.min(lastRed, lastBlue);
pBlue = cost[2] + Math.min(lastRed, lastGreen);
}
return Math.min(pRed, Math.min(pGreen, pBlue));
}
}

Reference:

https://leetcode.com/discuss/51721/simple-java-dp-solution

256. Paint House的更多相关文章

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

  2. [LeetCode#256] Paint House

    Problem: There are a row of n houses, each house can be painted with one of the three colors: red, b ...

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

  4. [LeetCode] 256. Paint House_Easy tag: Dynamic Programming

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  5. 256. Paint House房屋染色

    [抄题]: There are a row of n houses, each house can be painted with one of the three colors: red, blue ...

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

  7. [LC] 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 ...

  8. 【LeetCode】256. Paint House 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

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

随机推荐

  1. Firebird数据库相关备忘录

    Firebird数据库中有一些很特别的东西,很好用,但由于平时用的不多,记在这里,以备以后用到时查询. 1.以ADO 的OLE ODBC驱动方式访问 Firebird,可以使用如下连接串: FBCon ...

  2. Mysql备份--mysqldump&outfile

    1.备份工具mysqldump 客户端和服务器端都能用select outfile 只能写到服务器端 2.按表单位备份 a.单个表备份 mysqldump -uusername -p database ...

  3. 0x03伪指令

    等号伪指令 = 相当于指定常量,由等号定义的符号常量不占用存储空间. count = 1234 可以重复定义多次,EQU则不容许 EQU伪指令 1.常量名 EQU 表达式 NUMBER EQU 10* ...

  4. NSRange、NSPoint(CGPoint)、NSSize(CGSize)、NSRect(CGRect)

    1.NSRange: typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; NSRange本身是系统 ...

  5. tomcat内存溢出,设置

    tomcat/bin/catalina.bat里找到echo Using CATALINA_BASE:   "%CATALINA_BASE%" ,在上方设置:    set JAV ...

  6. kali linux安装vm

    https://download3.vmware.com/software/wkst/file/VMware-Workstation-Full-10.0.2-1744117.i386.bundle v ...

  7. git如何ignore

    今天新建了一个项目传到git上,但是每次编译都会有一些无用的文件生成,于是就编写了ignore.但是发现无用.因为你的文件已经上传到服务器了,再编写ignore就无用了,ignore的适用是文件没上传 ...

  8. 20160727noip模拟赛zld

    首先最优策略肯定是这样的:我们取出这个序列中的最大值,然后将整个序列分为左右两部分, 那么我们一定先把左右两部分合起来然后再与这个值合并 那么我们可以得出一个基于最值查询(rmq)的的算法,但是zld ...

  9. 当IIS挂的网站出现选 图片文件, 静态文件都打不开时, 可以试试新建一个应用程序池试试看...

    当IIS挂的网站出现选 图片文件, 静态文件都打不开时, 可以试试新建一个应用程序池试试看...

  10. PE文件结构详解(六)重定位

    前面两篇 PE文件结构详解(四)PE导入表 和 PE文件结构详解(五)延迟导入表 介绍了PE文件中比较常用的两种导入方式,不知道大家有没有注意到,在调用导入函数时系统生成的代码是像下面这样的: 在这里 ...