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. VMWAR-workstatuon : 安装win10、server 2008 r2、server 2012 r2

    最新版的VMWAR 不是很文档,建议大家还是下载稳定版,截止当前最新版的为15,用了,有点问题. 换成14~ 可以了.(15创建虚拟机安装vmware tools 怎么都安装不了). 关于创建虚拟机, ...

  2. spring-boot-starter-thymeleaf对没有结束符的HTML5标签解析出错

    springboot 在使用thymeleaf 作为模板时,当出现未关闭标签时,如下所示代码,标签没有关闭. <link href="plugin/layui/css/layui.cs ...

  3. Android为TV端助力 自定义通知栏

    package com.example.mvp; import cn.ljuns.temperature.view.TemperatureView;import presenter.ILoginPre ...

  4. (办公)eclipse连接github cannot open git-upload-pack(git-receive-pack)

    原文地址:https://blog.csdn.net/royal__moon/article/details/79427431 打开eclipse安装目录下的eclipse.ini添加一句:-Dhtt ...

  5. Spark操作parquet文件

    package code.parquet import java.net.URI import org.apache.hadoop.conf.Configuration import org.apac ...

  6. 获取Bing每日图片API接口

    bing图片每日更新,对于这一点感觉挺不错的,如果能够把bing每日图片作为博客背景是不是很不错呢?首先我们进入Bing首页,会发现自动转到中国版.不过这没关系,中国版更符合国情,速度也比国际版快一些 ...

  7. Powershell远程执行命令

    $Username = 'xx' $Password = 'xx' $ComputerName='xx' $pass = ConvertTo-SecureString -AsPlainText $Pa ...

  8. SQL Server 2012 手动安装帮助文档+排错

    逆天SQL Server 2012装的不要不要的,最后发现...竟然没帮助文档...汗啊!原来它跟vs一样要自己装帮助文档...好吧,官网一下载,妹的...报错...然后就让我们还原这个安装过程以及逆 ...

  9. window scoop 修改默认安装路径

    1.运行powershell  [environment]::setEnvironmentVariable('SCOOP_GLOBAL','F:\GlobalScoopApps','Machine') ...

  10. Windows平台搭建-----C语言

    上期我们已经进行Linux的平台搭建,今期我们就来搭建下我们最常用的.最适合初学者的一种方式,那就是搭建Windows平台开发环境,只需要两种工具即可,一个就是编辑器(编辑代码的工具),另一个就是编译 ...