原题链接在这里: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 IIPaint Fence.

LeetCode Paint House的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode Paint House II

    原题链接在这里:https://leetcode.com/problems/paint-house-ii/ 题目: There are a row of n houses, each house ca ...

  5. LeetCode Paint Fence

    原题链接在这里:https://leetcode.com/problems/paint-fence/ 题目: There is a fence with n posts, each post can ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 【转】详解Java正则表达式语法

    (转自: http://www.jb51.net/article/76354.htm) 这篇文章主要介绍了Java正则表达式语法,包括常用正则表达式.匹配验证-验证Email是否正确以及字符串中查询字 ...

  2. Ubuntu防火墙设置

    转载自:http://baisongfly.blog.163.com/blog/static/30135117200923005956159/ 1.安装 sudo apt-get install uf ...

  3. Power BI for Office 365(一)移动端应用

    此篇来自于微软商业智能网站的官方博客团队发布的Power BI在线资料其中的一部分,完整版地址: http://office.microsoft.com/en-us/office365-sharepo ...

  4. Source Insight编辑器配置

    Sublime Text 无疑是一款很优秀的编辑器和阅读器,可惜对于中文编码不支持,网上的ConvertTOUTF8存在BUG,经常转码失败,体验很不好. 现在开始使用source insight,这 ...

  5. JavaScript闭包深入解析

    for (var i=1; i<=5; i++) { setTimeout( function timer() { console.log( i ); }, i*1000 ); } --上面这段 ...

  6. iOS ---不一样的NSLog打印(精准打印)

    在iOS开发过程中,调试是很重要的过程,而除了各种断点调试(普通断点.条件断点.全局断点)之外,似乎NSLog是我们调试最常用的方法,当然,也是最简单朴素的寻debug方法. 在项目中,我们常使用的N ...

  7. Android自动化测试 - Robotium之re-sign.jar重签名后安装失败提示Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES]解决方案

    问题:在用re-sign.jar重签名apk文件后,显示重签名成功,但在实际安装过程中确提示:Failure [INSTALL_PARSE_FAILED_NO_CERTIFICATES] 原因:网上查 ...

  8. [转]webpack进阶构建项目(一)

    阅读目录 1.理解webpack加载器 2.html-webpack-plugin学习 3.压缩js与css 4.理解less-loader加载器的使用 5.理解babel-loader加载器 6.理 ...

  9. 【JAVA】Runtime

    1.内存管理:Java提供了无用单元自动收集机制.通过totalMemory()和freeMemory()方法可以知道对象的堆内存有多大,还剩多少.Java 会周期性的回收垃圾对象(未使用的对象),以 ...

  10. 实现倒计时功能js

    <p>系统将会在<strong id="endtime"></strong>秒后跳转到登录页!</p> [原生js实现] <s ...