https://leetcode.com/problems/house-robber/

题意:

一维数组,相加不相邻的数组,返回最大的结果。

思路:

一开始思路就是DP,用一维数组保存dp[i]保存如果偷第i间,此时可偷到多少。DP的方向不太好,所以效率很低。

Runtime: 4 ms, faster than 17.53%

class Solution
{
public:
int rob(vector<int> &nums)
{
int res = ;
int len = nums.size();
if (len <= )
return res;
int dp[len];
for (int i = ; i < len; i++)
{
dp[i] = nums[i];
res = max(res, dp[i]);
}
if (len > )
{
dp[] += dp[];
res = max(res, dp[]);
}
for (int j = ; j < len; j++)
{
dp[j] += max(dp[j - ], dp[j - ]);
res = max(res, dp[j]);
}
return res;
}
};

后面DP思路改成:dp[i]记录在偷到第i位时,最大可偷多少钱。

可偷最多的钱要么是偷这次的,要么是不偷这一次的。

转移方程为: dp[i]=max(dp[i-]+nums[i],dp[i-])

Runtime: 0 ms, faster than 100.00%

class Solution
{
public:
int rob(vector<int> &nums)
{
int res = ;
int len = nums.size();
if (len <= )
return res;
if (len == )
return nums[];
if (len == )
return (nums[] > nums[] ? nums[] : nums[]);
int dp[len];
dp[] = nums[];
dp[] = max(nums[], nums[]); for (int i = ; i < len; i++)
{
dp[i] = max(dp[i - ] + nums[i], dp[i - ]);
}
return dp[len - ];
}
};

leetcode 198. House Robber (Easy)的更多相关文章

  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. (easy)LeetCode 198.House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  3. [LeetCode] 198. House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  4. Leetcode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  5. Java for LeetCode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  6. Java [Leetcode 198]House Robber

    题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

  7. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. Leetcode 198 House Robber 动态规划

    题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...

  9. leetcode 198 House Robber I

    function rob(nums) { if(!nums || nums.length === 0) { return 0; } else if(nums.length < 2){ retur ...

随机推荐

  1. 纯CSS3创意loading文字特效

    快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中.   <HTML开发Mac OS App 视频教程> 土豆网同步更新:http: ...

  2. Openssl - Static libraries (w32, mingw) 以及对Qt静态编译时的设置

    Openssl static libraries created for Windows 32bit using MinGW compiler   Compiled with:       ./Con ...

  3. 编译icu库(用到了cygwin)

    源码下载 icu项目地址 安装cygwin,至少安装以下几个工具 make dos2unix binutils 编译工程 打开命令行,进入根目录的 source 文件夹 配置VC编译环境,执行命令 “ ...

  4. c# 查询本机可用的代理ip

    现在有很多网站都提供免费的代理ip,但是你会发现很多网站显示的可以用的 ,在自己电脑上是用不了,写个小代码提取出自己电脑上可以用的代理,没什么技术含量,只是为了记录一下 string strUrl = ...

  5. 简洁的描述SpringMVC工作流程

    1.客户端发送来的请求 经过前端控制器(springDispatcherServlet)调用映射器(HandlerMapping)来找到对应的执行链(HandlerExecutionChain)对象, ...

  6. 你一定能看懂的JDK动态代理

    前言:阅读这篇文章前,一定要知道什么是代理模式,具体可以参考这篇文章<设计模式(一):代理模式>. 在<设计模式(一):代理模式>一文中说了,程序员思思买书有两种选择:一种是选 ...

  7. UI-grid 表格内容可编辑(enableCellEdit可指定列编辑)

    在网上搜索了很多关于UI-Grid的问题 很遗憾好少啊啊啊 不过有API还是比较欣慰的 官方API:UI Grid 还有一位大佬的翻译的中文API:angularjs ui-grid中文api 行编辑 ...

  8. Storm 学习之路(九)—— Storm集成Kafka

    一.整合说明 Storm官方对Kafka的整合分为两个版本,官方说明文档分别如下: Storm Kafka Integration : 主要是针对0.8.x版本的Kafka提供整合支持: Storm ...

  9. linux下svn安装

    1.环境centos6.4 2.安装svnyum -y install subversion 3.配置 建立版本库目录mkdir /www/svndata svnserve -d -r /www/sv ...

  10. 自己实现AOP,AOP实现的步骤分解

    自己实现简易的AOP 一.需求:自己实现AOP:1.0版本:在某个方法上加"@InOutLog"注解,那么执行到该方法时,方法的前面.后面会输出日志信息. [自己实现AOP 2.0 ...