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. js 控制超出字数显示省略号

    //多余显示省略号 function wordlimit(cname, wordlength) { var cname = document.getElementsByClassName(cname) ...

  2. POJ3734 Blocks(生成函数)

    题意 链接 长度为\(n\)的序列,用红黄蓝绿染色,其中红黄只能是偶数,问方案数 Sol 生成函数入门题 任意的是\(e^x\),偶数的是\(\frac{e^x + e^{-x}}{2}\) 最后化完 ...

  3. springboot Redis 缓存

    1,先整合 redis 和 mybatis 步骤一: springboot 整合 redis 步骤二: springboot 整合 mybatis 2,启动类添加 @EnableCaching 注解, ...

  4. redis redis常用命令及内存分析总结(附RedisClient工具简介

    redis常用命令及内存分析总结(附RedisClient工具简介 by:授客 QQ:1033553122 redis-cli工具 查看帮助 连接redis数据库 常用命令 exists key se ...

  5. Python高级特性:切片

    切片的目的是实现取一个list或tuple的部分元素 学习自廖雪峰,个人理解如下: 取列表L的前三个元素 >>> L = ['Michael', 'Sarah', 'Tracy', ...

  6. Python基础知识点

    自学记录: 1.字符串 python中单引号和双引号使用完全相同. 使用三引号('''或""")可以指定一个多行字符串. 转义符 '\' 反斜杠可以用来转义,使用r可以让 ...

  7. 章节七、1-ArrayList

    一.集合是一个容器,前面讲的数值也是一个容器, 它们的区别是: 1.数组既可以存储基本数据类型,又可以存储引用数据类型,而集合只能存储引用数据类型,也就是对象. 2.基本数据类型存储的是值,引用数据类 ...

  8. matlab练习程序(波纹扭曲)

    其实就是用sin或cos对x,y坐标进行变换,处理的时候依然是反向变换. 类似的,用不同的函数能得到不同的扭曲效果,比如log,1/x,exp等等. 效果如下: 代码如下(还给出了如何生成gif图片的 ...

  9. SSM框架—环境搭建(MyEclipse+Tomcat+MAVEN+SVN)

    1.JDK的安装 首先下载JDK,这个从sun公司官网可以下载,根据自己的系统选择64位还是32位,安装过程就是next一路到底.安装完成之后当然要配置环境变量了. 1.1新建变量名:JAVA_HOM ...

  10. SQL 删除外键列

    一 SQL删除列的语句是: alter table tableName drop column columnName --(其中,tableName为表名,columnName为列名) 但是,如果某列 ...