Paint House

自己的写法:

class Solution {
public:
/**
* @param costs: n x 3 cost matrix
* @return: An integer, the minimum cost to paint all houses
*/
int minCost(vector<vector<int> > &costs) {
// write your code here
int length = costs.size();
vector<vector<int> > dp(length + ,vector<int>());
for(int i = ;i < ;i++)
dp[][i] = ;
for(int i = ;i <= length;i++){
dp[i][] = min(dp[i-][] + costs[i-][],dp[i-][] + costs[i-][]);
dp[i][] = min(dp[i-][] + costs[i-][],dp[i-][] + costs[i-][]);
dp[i][] = min(dp[i-][] + costs[i-][],dp[i-][] + costs[i-][]);
}
int min_num = INT_MAX;
for(int i = ;i < ;i++){
min_num = min(min_num,dp[length][i]);
}
return min_num;
}
};

更优的写法:

https://www.cnblogs.com/grandyang/p/5319384.html

class Solution {
public:
int minCost(vector<vector<int>>& costs) {
if (costs.empty() || costs[].empty()) return ;
vector<vector<int>> dp = costs;
for (int i = ; i < dp.size(); ++i) {
dp[i][] += min(dp[i - ][], dp[i - ][]);
dp[i][] += min(dp[i - ][], dp[i - ][]);
dp[i][] += min(dp[i - ][], dp[i - ][]);
}
return min(min(dp.back()[], dp.back()[]), dp.back()[]);
}
};

lintcode 515. 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. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  3. [LintCode] Paint Fence 粉刷篱笆

    There is a fence with n posts, each post can be painted with one of the k colors.You have to paint a ...

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

  6. 【ZOJ - 3780】 Paint the Grid Again (拓扑排序)

    Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...

  7. 详解Paint的setXfermode(Xfermode xfermode)

    一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...

  8. android Canvas 和 Paint用法

    自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...

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

随机推荐

  1. canvas-a13prototype.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 2018-08-24 中文代码之Spring Boot对H2数据库简单查询

    续前文: 中文代码之Spring Boot集成H2内存数据库 在词条中添加英文术语域: @Entity public class 词条 { @Id private long id; private S ...

  3. Asp.Net MVC 利用ReflectedActionDescriptor判断Action返回类型

    System.Web.Mvc.ReflectedActionDescriptor descriptor = filterContext.ActionDescriptor as System.Web.M ...

  4. Android项目实战(五十一):浅谈GreenDao

    比较出名的数据库框架 GreenDao使用步骤: 1.app目录下的build.gradle文件 添加依赖 compile 'org.greenrobot:greendao:3.2.0' 顶部添加插件 ...

  5. float、double、BigDecimal的一些精度问题

    float f = 280.8f;System.out.println(f*100);结果是什么?结果是:28080.0f(我是这么想的)实际结果是:28079.998 既然float处理有问题换do ...

  6. Visual Studio语言设置

    按照的是中文的visual studio,用起来很不方便,因为程序员的都是英文版,平时交流时也是英文的名字 转换语言时发现只有中文和跟随windows系统的设置 官方给的文档看的不是很清楚 查阅资料后 ...

  7. SQL Server 锁实验(SELECT加锁探究)

    本例中使用begin tran和with (holdlock)提示来观察SQL Server在select语句中的锁. 开启事务是为了保证时间极短的查询也能观察到锁情况,holdlock相当于开启序列 ...

  8. Python赋值运算符

    赋值运算符 运 算 符  说 明 举 例   展 开 形 式  =   简单的赋值运算  x=y x=y  +=  加赋值 x+=y  x=x+y -=  减赋值 x-=y  x=x-y   *= 乘 ...

  9. oracle 压力测试工具benchmarksql

    TPC-C测试 TPC-C 于 1992 年 7 月 23 日认可为新的基准测试.TPC(Transaction Processing Performance Council,事务处理性能委员会)-C ...

  10. NumPy 中的集合运算

    怎样快速找出两个数组中相同的元素? numpy.isin(element,test_elements,assume_unique = False,invert = False ) 计算test_ele ...