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 ...
随机推荐
- Day20160425
技术要求: 1.git使用 pull.push.cheakout.master.clone(本地提交有优势) 2.Maven(依赖init.install.compile.package.clean. ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- win10中将默认输入法设置为英文
开始 设置 时间和语言 区域和语言 语言--中文--选项 微软拼音输入法--选项 IME默认模式--英语
- iOS--UIScrollView图片动画切换【实现每次只加载3张图片,进而减少占用内存,可循环滚动】
#import "ViewController.h" #define IMAGENUMBER 5 #define SIZE self.view.bounds.size @inter ...
- Android入门(七):Spinner下拉式菜单组件
对于手机和平板电脑的应用程序来说,打字是非常不方便的操作方式,比较好的方式就是列出一组选项让用户挑选,这样就可以避免打字的麻烦.使用Spinner下拉菜单组件需要完成以下几个步骤: 1.建立选项列表, ...
- jQuery 利用 parent() parents() 寻找父级 或祖宗元素
$(this).parent().parent().parent().parent().parent().remove(); //此方法通过parent()一级一级往上找 $(this).pare ...
- HTML结构化
目的:为开发页面时有一套明确的页面结构化实施方案,提高开发效率: HTML结构化指的其实就是使用HTML语义化标签根据web标准书写具有明确结构逻辑的HTML代码的一种思路: 说白了重点就是:页面实际 ...
- Windows Phone Sliding Effect
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Backgro ...
- DOS 命令For精解示例
最基本形态: 在cmd 窗口中:for %I in (command1) do command2 在批处理文件中:for %%I in (command1) do command2 在批处理中,FOR ...
- extjs后自己写了一些见不得人的脚本
<html> <head> <title> 配置管理器 </title> <style type="text/css"> ...