256. Paint House
题目:
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的更多相关文章
- 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:二叉树下的不能相邻,求能 ...
- [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 ...
- [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 ...
- [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 ...
- 256. Paint House房屋染色
[抄题]: There are a row of n houses, each house can be painted with one of the three colors: red, blue ...
- [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 ...
- [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 ...
- 【LeetCode】256. Paint House 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [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 ...
随机推荐
- Python中处理时间 —— time模块
time模块 time 模块可用来处理时间,详细的说明参考 time模块说明. 逝去的秒数 逝去的秒数表示从某个时间(Python中是"Thu Jan 1 07:00:00 1970&quo ...
- 设计模式之单例模式(Singleton Pattern)
单例模式 单例模式(Singleton Pattern)在java中算是最常用的设计模式之一,主要用于控制控制类实例的数量,防止外部实例化或者修改.单例模式在某些场景下可以提高系统运行效率.实现中的主 ...
- git,repo学习
Repo:就是一组git命令的集合,repo init 下载一个分支. repo start 文件名 --all本地传建的另一个代码分支,用于备份作用. 比如:repo start zhao --al ...
- str_replace使用array替换
<?php //替换采集等通过url参数传值 function admin_ff_url_repalce($xmlurl,$order='asc'){ if($order=='asc'){ re ...
- Javascript实例:求数组中最大、最小值及下标
题目:定义一个数组,并给出7个整数,求该数组中的最大值,及最大值下标,最小值及最小值下标.<script type="text/javascript">//定义一个数组 ...
- Android学习笔记:TabHost 和 FragmentTabHost(转)
转自:http://www.cnblogs.com/asion/p/3339313.html 作者:Asion Tang 出处:http://asiontang.cnblogs.com T ...
- MVC与WebForm的一些区别
MVC与WebForm的一些区别 它们都是ASP.NET WEB开发的两种方式 .但是他们也是有一些不同.做个小结. 1.MVC是没有服务器端控件这么一说的,也就是没有viewstate,也就不会产生 ...
- notifyDataSetChanged listview内容没更新的问题
如红色部分所示,需在Adapter添加setData方法,当 listData中数据更改后,调用setData,为Adapter设置新的数据,此时调用notifyDataSetChanged() 就可 ...
- 【UVA】【11427】玩纸牌
数学期望 也是刘汝佳老师白书上的例题……感觉思路很神奇啊 //UVA 11427 #include<cmath> #include<cstdio> #include<cs ...
- .run文件安装
比如realplay.run 安装方法如下 chmod +x realplay.run ./realplay.run 然后他就会执行安装了,在过程中可能会要求你输入yes或no 安装完后就可以用了