[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为要用i , j的二维矩阵:就3种情况,枚举所有情况就行了

[一句话思路]:

求和取前一步最小值,从而实现步步最小

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 理解过程:把所有值都累加到nums[n]中

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

求和类型的三步dp,套用坐标型模板

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

标准格式i = 0,i < n,return f[n - 1]

for (int i = 1; i < n; i++) {
costs[i][0] += Math.min(costs[i - 1][1], costs[i - 1][2]);
costs[i][1] += Math.min(costs[i - 1][0], costs[i - 1][2]);
costs[i][2] += Math.min(costs[i - 1][0], costs[i - 1][1]);
}
//answer
return Math.min(costs[n - 1][0], Math.min(costs[n - 1][1],costs[n - 1][2]));

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

265. Paint House II 数学问题,感觉并没有什么联系

[代码风格] :

public class Solution {
/**
* @param costs: n x 3 cost matrix
* @return: An integer, the minimum cost to paint all houses
*/
public int minCost(int[][] costs) {
//corner case
if (costs.length == 0 || costs[0].length == 0) {
return 0;
}
//get sum
int n = costs.length;
for (int i = 1; i < n; i++) {
costs[i][0] += Math.min(costs[i - 1][1], costs[i - 1][2]);
costs[i][1] += Math.min(costs[i - 1][0], costs[i - 1][2]);
costs[i][2] += Math.min(costs[i - 1][0], costs[i - 1][1]);
}
//answer
return Math.min(costs[n - 1][0], Math.min(costs[n - 1][1],costs[n - 1][2]));
}
}

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. 256. Paint House

    题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue o ...

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

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

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

  6. lintcode - 房屋染色

    class Solution { public: /* * @param costs: n x 3 cost matrix * @return: An integer, the minimum cos ...

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

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

  9. 【LeetCode】256. Paint House 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

随机推荐

  1. 【转】HP laserjet p2055dn的自动双面打印功能

    原文网址:http://zhidao.baidu.com/link?url=n_NW7Qfa_7HlrEhLucdvKO43jj3SpFXJhGAfQ-WqF979jm80eUv8s1atqtxE7w ...

  2. ASP.NET比较常用的26个性能优化技巧

    1. 数据库访问性能优化 数据库的连接和关闭 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器资源.ASP.NET中提供了连接池( ...

  3. RK3288 查看时钟树

    主控端可以通过指令查看时钟树,enable_cnt为1,表示时钟已使能. # cat d/clk/clk_summary cat d/clk/clk_summary clock enable_cnt ...

  4. spring AOP 之五:Spring MVC通过AOP切面编程来拦截controller

    示例1:通过包路径及类名规则为应用增加切面 该示例是通过拦截所有com.dxz.web.aop包下的以Controller结尾的所有类的所有方法,在方法执行前后打印和记录日志到数据库. 新建一个spr ...

  5. [转]加密经验集 => C#

    下载地址 代码摘自 C#高级编程(第7版) 第579页 不对称加密

  6. Windows Server 2012十大实用快捷键组合

    在本文中,我们将一起体验快捷键如何在微软最新服务器操作系统中帮助用户提升工作效率. 微软推出的最新服务器操作系统比我印象中任何一款前代Windows Server产品都依赖于键盘操作——当然,这些产品 ...

  7. GITBOOK/HEXO TRAVIS GITHUB-PAGES 博客搭建

    简介 这年头要是没有个博客都不好意思给别人说你是程序员,我用XX笔记呀,不行吗?不行,这玩意儿要么不能公开分享,要么公开分享要会员,现在到处都是开源,自己学到了东西都不能分享给需要帮助的人,真是伤心呀 ...

  8. python学习(二十二) Python 中boolean

  9. Codeforces-708C(树形DP)

    一.题意 给定一颗树,对于每一个节点,判断能否在树中删除某一条边,然后在任意两个节点之间加一条边,使这个点成为重心. 注:删除树中某一条边后,标程并不会这么无聊地把这棵树变成两个孤立的连通图,而是再让 ...

  10. [POJ] The Triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47278   Accepted: 28608 De ...