lintcode 515. Paint House
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的更多相关文章
- 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:二叉树下的不能相邻,求能 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- [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 ...
- [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 ...
- [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 ...
- 【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 ...
- 详解Paint的setXfermode(Xfermode xfermode)
一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...
- android Canvas 和 Paint用法
自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...
- [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 ...
随机推荐
- IE6不兼容问题
IE6不兼容问题 一.选择器兼容问题 1.交集选择器从IE7以上兼容(div.special): 2.儿子选择器(>):IE7开始兼容,IE6不兼容. 3.序选择器(first ...
- JS里面的装箱和拆箱操作
平日工作里,我想各位少侠对下面的用法都不陌生吧 var s1 = "abc"; var s2 = s1.indexOf("a") 还有例如什么indexOf() ...
- Flutter 布局(二)- Padding、Align、Center详解
本文主要介绍Flutter布局中的Padding.Align以及Center控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Padding A widget that insets ...
- 【效率工具】史上最好用的SSH一键登录脚本,超强更新!
说明 虽然已经是凌晨,但丝毫不能掩盖我激动的心情,今天完成了对GotoSSH的一次大更新,新增了两个肥肠实用的功能,我只能说,是真的好用,话不多说,先来看效果图: 普通的一键登录: 一键登录跳板机,然 ...
- event.target和event.currentTarget区别
首先本质区别是: event.target返回触发事件的元素 event.currentTarget返回绑定事件的元素
- ssh框架总结之action接收参数的三种方式
页面将参数传递给action的三种方式 一是通过属性传值: 将页面和action的的属性值保持一致,在action上写上该属性的set和get方法,这样在页面提交参数的时候,action就会调用set ...
- PyCharm 在PyCharm中使用GitHub
PyCharm是当前进行Python开发,尤其是Django开发最好的IDE,GitHub是程序员的圣地,几乎人人都在用,就不详细介绍两者了. 本文假设你对PyCharm和Github都有一定的了解, ...
- Android 官方Demo ActionBarCompat-Styled
ActionBarCompat-Styled Demo下载地址:https://github.com/googlesamples/android-ActionBarCompat-Styled/#rea ...
- Java 关键字 速查表
访问控制:private 私有的protected 受保护的public 公共的 类.方法和变量修饰符abstract 声明抽象class 类extends 扩允,继承final 终极,不可改变的im ...
- 第二章 基础查询 2-1 SQL语句基础
一.列的查询 基本的SELECT语句: SELECT <列名 >,...... FROM < 表名>; 注:子句是SQL的组成要素. 注:查询结果中的列的顺序和SELECT子句 ...