题目:

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. 虚拟局域网VLAN

    6.5.1配置路由器广域网端口的PPP封装 (1)配置路由器A: Router>enable Router#config Router_config#hostname Router-A Rout ...

  2. 11g RAC R2 体系结构---Grid

    基于agent的管理方式 从oracle 11.2开始出现了多用户的概念,oracle开始使用一组多线程的daemon来同时支持多个用户的使用.管理资源,这些daemon叫做Agent.这些Agent ...

  3. selenium for python 所有方法

    先列出selenium所有方法,然后挨个使用!说明 add_cookieapplication_cachebackcapabilitiesclosecommand_executorcreate_web ...

  4. [转] 浅谈Microsoft MVP

    微软MVP,这个自1993 年开始在社群上出现的计划(MVP Award Program),目前在全球已经累积超过5,000 人,其中在台湾已经有一百多人了,包括我在内,这个计画现在已经成为以微软技术 ...

  5. eclipse安装pydev插件

    打开Eclipse,找到Help菜单栏,进入Install New Software…选项. 点击work with:输入框的旁边点击Add…,Name可以随便输入,Location是http://p ...

  6. Oracle计算两个整数的和与这两个整数的差与商

    PL/SQL(Procedural Language/SQL)是一种过程化语言. PL/SQL都是以(BLOCK)块为基本单位,整个PL/SQL块分为三部分 1.声明(Declare) 2.执行(以B ...

  7. 项目文件包含 ToolsVersion="12.0" 设置,而此版本的 MSBuild 不支持该工具版本

    解决方法: 右键点击你的项目,选择属性,再点击配置属性中的常规,常规中有个平台工作集,把V120改成V100,点击应用即可.

  8. ios项目绕过证书访问https程序

    如果是单个的webview或者request请求,在请求的文件h中直接实现NSURLConnectionDelegate,并在m中添加下列实现下列两个方法: - (BOOL)connection:(N ...

  9. python学习小结2:if和while控制语句

    if语句 if语句中,代码块是按缩进的空格数量来判断的,也就是说空格数量一致的相邻行会被当作一个代码块,当if的条件成立的时候它就会得到执行. x = 100 if x > 50: print ...

  10. Windows下将txt导入MySQL及远程连接设置

    1.修改字符编码,全部修改为gbk.这样修改,重启后又会恢复默认值. show variables like '%char%'; set character_set_database=gbk; 其中, ...