题目:

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杂记-6(time&datetime模块)

    #!/usr/bin/env python# -*- coding: utf-8 -*-import timeprint(time.clock())##返回处理器时间,3.3开始已废弃 , 改成了ti ...

  2. WPF 气泡尖角在左边、下面、右边、上面

    由于项目需要,在弄一个气泡提示框,根据网上资料,使用Path可以将气泡画出来,下面是我画出来的. 1.气泡尖角在左边的: <Path Stroke="Black" Strok ...

  3. C# 获得手机归属地功能

    今天通过查资料了解到web的页面抓取功能,应用HttpWebRequest和HttpWebResponse功能,从http://www.showji.com网站中抓取归属地信息 应该说这个方法是从别的 ...

  4. SQL拼接备份数据库

    在SQLserver使用脚本备份数据库的时候需要注意的问题是: 1.指向的文件名必须是有读写权限. 2.在使用批量数据库备份时候需要根据自己需求选择性备份. -- ================== ...

  5. 34 个使用 Raspberry Pi 的酷创意

    如果你手头有一个 Raspberry Pi(树莓派),你会拿它来做什么?或许以下 34 个如何使用 Raspberry Pi 的创意能够给你带来一些启发. Web 服务器 家庭自动化 BitTorre ...

  6. POJ 1195 2维线段树(树套树实现) 树状数组

    1: #include <stdio.h> 2: #include <string.h> 3: #include <stdlib.h> 4: #include &l ...

  7. cocos3.2版本中的一些新特性

    1.设置屏幕分辨率的大小,需要手动添加: 2.去掉了所有CC开头的命名: 3.所有的单例(以前是采用shared开头方法),全部改为getInstance(); 4.cocos3.x以上的版本支持C+ ...

  8. audio 设置 currentTime 失效 的解决办法

    当服务端返回的 音频文件标示 no-cache 的时候,会引起currentTime 失败. 改掉server 返回头信息.解除禁止缓存,一切ok.

  9. 设计模式之单实例模式(Singleton)

    原理:将类的构造函数由pubic变为private或者protect,添加获取对象的public 成员函数,返回指向对象的静态指针. 首先来一段简单的代码实现 代码一 class Singleton ...

  10. cg 到hlsl的转换

    http://msdn.microsoft.com/en-us/library/windows/desktop/ff471376(v=vs.85).aspx http://gamedev.stacke ...