题目:

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. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  2. 1094. The Largest Generation (25)

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  3. 关于使用视图进行分页时出现当前记录集不支持书签的错误解决方法及原因(asp)

    一般在使用视图进行查询时,视图中意般都关联了两个或者更多个表,一般在这种情况下才会使用视图,但是但我在使用视图来查询数据时没有问题,但是一旦在分页中使用到视图进行查询就会出现错误提示如下: ADODB ...

  4. ASP.NET MVC +EasyUI 权限设计(一)开篇

    在前一段时间中,老魏的确非常的忙碌,Blog基本上没有更新了,非常的抱歉,那么在后面的时间中,老魏会尽量的抽时间来写的,可能时间上就不太富裕了.今天开始呢,老魏会和大家分享一下关于权限设计的有关文章, ...

  5. [转载]iOS面试题总

    转载自:http://blog.sina.com.cn/s/blog_67eb608b0101r6xb.html (2014-06-13 20:23:33) 转载▼ 标签: 转载   crash 原文 ...

  6. NodeJS从零开始——NPM的使用

    NPM是一个Node包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准.有了NPM,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. NPM常用的命令有: (1)$ ...

  7. swift 类 与 结构体

    这两天突然有人问我  swift里面 类和 结构体  有什么区别? 说实在的本人目前不太看好swift,相信很多人也是,oc 都 很成熟了. 本人目前不打算深入了解swift的原因swift  语言 ...

  8. 华为章宇:如何学习开源项目及Ceph的浅析

    转自http://www.csdn.net/article/2014-04-10/2819247-how-to-learn-opensouce-project-&-ceph 摘要:开源技术的学 ...

  9. mysql获取日期(将时间戳转换成短日期格式)

    且看如下: '; 结果: +-------------------------------------+---------------------+ | date_format(create_time ...

  10. segment fault

    http://blog.chinaunix.net/uid-23069658-id-3959636.html