LeetCode Paint House
原题链接在这里:https://leetcode.com/problems/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.
题解:
DP问题. 类似House Robber. red = 当前房子paint red cost + Math.min(之前paint blue的总花费,之前paint green的总花费). blue 和 gree 同理.
Time Complexity: O(n). Space: O(1).
AC Java:
public class Solution {
public int minCost(int[][] costs) {
if(costs == null || costs.length == 0){
return 0;
}
int len = costs.length;
int red = 0; //最后print red 时的总共最小cost
int blue = 0;
int green = 0;
for(int i = 0; i<len; i++){
int preRed = red;
int preBlue = blue;
int preGreen = green;
red = costs[i][0] + Math.min(preBlue, preGreen); //当前house选择red, 之前只能在blue 和 green中选
blue = costs[i][1] + Math.min(preRed, preGreen);
green = costs[i][2] + Math.min(preRed, preBlue);
}
return Math.min(red, Math.min(blue, green));
}
}
跟上Paint House II, Paint Fence.
LeetCode Paint House的更多相关文章
- [LeetCode] Paint Fence 粉刷篱笆
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- [LeetCode] 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 ...
- [LeetCode] 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 Paint House II
原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...
- LeetCode Paint Fence
原题链接在这里:https://leetcode.com/problems/paint-fence/ 题目: There is a fence with n posts, each post can ...
- [LeetCode] Paint House I & II
Paint House There are a row of n houses, each house can be painted with one of the three colors: red ...
- [LeetCode#265] Paint House II
Problem: There are a row of n houses, each house can be painted with one of the k colors. The cost o ...
- [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#276] Paint Fence
Problem: There is a fence with n posts, each post can be painted with one of the k colors. You have ...
随机推荐
- PHP基础之POST与GET
post 与 get区别 *.Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示.*.Post传输的数据量大,可以达到2M,而Get方法由于受到URL长度的限制,只能传递大约1 ...
- 查看cpu的信息cat /proc/cpuinfo
cat /proc/cpuinfo processor : vendor_id : GenuineIntel cpu family : model : model name : Intel(R) Co ...
- opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像
前言 好长时间没写博文了,今天偷偷懒写篇关于opencv2中鼠标响应操作的文章. 鼠标操作属于用户接口设计,以前一直使用Qt来做,但是如果只需要简单的鼠标,键盘操作,直接调用opencv库的函数也未尝 ...
- Matlab函数 meshgrid
- Oracle协议适配器错误解决办法
在Oracle中新建了一个数据库,今天把它删了之后再登录SQL*PLUS就登不上去了,出现ORA-12560:TNS:协议适配器错误. ORA-12560: TNS: 协议适配器错误的解决方法 造成O ...
- Daily Scrum Meeting ——ZeroDay(Beta)12.08
算是BETA冲刺的前奏,不算正式冲刺
- 简单CSS3动画制作
本贴已重新编辑至http://www.cnblogs.com/fastmover/p/4977358.html 最近需要用到了一些CSS3动画,基本用Animate.css(https://githu ...
- D3.js部署node环境开发
总结一段D3.js部署node环境的安装过程 准备阶段: 首先电脑上要安装node环境,这个阶段过滤掉,如果node环境都不会装,那就别玩基于node环境搞的其他东西了. 搭建环境: 我在自己的F:系 ...
- Excel转Json,Json转CSharp
一份给策划最好的礼物!就是:Excel2Json2CSharp 策划配置Excel,动不动就要改数值啊,增加字段啊. 程序这边对应的解析类就得改动啊.整一个麻烦了得! 所以我就整理了这个Excel2J ...
- js获取?后面具体参数的值
function getURLParam(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' ...