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.
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要用i , j的二维矩阵:就3种情况,枚举所有情况就行了
[一句话思路]:
求和取前一步最小值,从而实现步步最小
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 理解过程:把所有值都累加到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房屋染色的更多相关文章
- 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:二叉树下的不能相邻,求能 ...
- 256. Paint House
题目: There are a row of n houses, each house can be painted with one of the three colors: red, blue 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]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 ...
- lintcode - 房屋染色
class Solution { public: /* * @param costs: n x 3 cost matrix * @return: An integer, the minimum cos ...
- [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 ...
随机推荐
- 将SQLite移植到ARM板上 (转)
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它, 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够 ...
- XAMPP服务器在局域网只能本机访问且无法用IP访问的解决办法 (转)
XAMPP服务器在局域网只能本机访问且无法用IP访问的解决办法 前几天安装了xampp for pc 1.7.4版本. 装好后在本地电脑通过https://localhost访问正常. 然后换了台电脑 ...
- RK3288 修改设备默认的蓝牙名称
path:device/rockchip/rk3288/bluetooth/bdroid_buildcfg.h /* * Copyright (C) 2012 The Android Open Sou ...
- 常见企业IT支撑【7、keepalived VRRP双主master】
我们知道,最简单的keepalive vrrp作出来的VIP实例,征用了2台服务器,生成1个VIP,也就是说,基础实配置实例中,我们的业务流量只会流向其中一台服务器,另一台就空闲了,明显示合, 能否做 ...
- supervisor备忘
supervisor是把普通app变成deamon的工具,虽然没有erlang的supervise粒度那么细,但是已经非常方便了 安装 sudo apt-get install supervisor ...
- jq 合并json对象
一,保存object1和2合并后产生新对象,若2中有与1相同的key,默认2将会覆盖1的值 1 var object = $.extend({}, object1, object2); 二,将2的值合 ...
- Java 中的this关键字
1.this关键字代表当前对象 this.属性 操作当前对象的属性 this.方法 调用当前对象的方法 2.封装对象的属性的时候,经常会使用this关键字 public class Telphone ...
- mybatis实现继承映射
ORM 框架的优势在于能让我们利用面向对象的思维去操作数据库, hibernate 作为重量级的 ORM 框架对面向对象的支持很强大.作为半自动化的 mybatis ,对面向对象的支持也是很完备的.这 ...
- STL算法与树结构模板
STL算法 STL 算法是一些模板函数,提供了相当多的有用算法和操作,从简单如for_each(遍历)到复杂如stable_sort(稳定排序),头文件是:#include <algorithm ...
- C++四种强制转换
C++的四种强制类型转换,所以C++不是类型安全的.分别为:static_cast , dynamic_cast , const_cast , reinterpret_cast 为什么使用C风格的强制 ...