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. 解析 Qt 字库移植并能显示中文 (下篇)

    原文http://mobile.51cto.com/symbian-272563.htm 本文介绍的是Qt 字库移植并能显示中文,需要的字体库文件,一般是多个.具体移植那一个,看你使用的字库是什么了, ...

  2. QtZint编译过程记录(要使用 QTMAKE_CFLAGS += /TP 参数)

    1,下载zint后,在zint-2.4.3\win32\vcx目录下找到zlib.props和libpng.props文件,分别改为zlib和libpng的源码目录.这2个开源库最好是找工程中使用的版 ...

  3. TopFreeTheme精选免费模板【20130626】

    有一段时间没有发布的模板了,相信很多喜欢新模板的朋友有点焦急了!还好,今天我今天整理了13个最新的模板,主要是WordPress的,另外3个是关于Joomla的模板,他们分别是游戏主题.俱乐部主题以及 ...

  4. [2017.02.06] 阅读《Effective Morden C++》

  5. 条款16:成对使用new和delete时要使用相同的形式

    请牢记: 如果在new表达式中使用[],必须在相应的delete表达式中也使用[]. new[]  对应  delete[] 如歌在new表达式中不适用[],一定不要在相应的delete表达式中使用[ ...

  6. KNN算法——分类部分

    1.核心思想 如果一个样本在特征空间中的k个最相邻的样本中的大多数属于某一个类别,则该样本也属于这个类别,并具有这个类别上样本的特性.也就是说找出一个样本的k个最近邻居,将这些邻居的属性的平均值赋给该 ...

  7. spring 5.x 系列第13篇 —— 整合RabbitMQ (xml配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 本用例关于rabbitmq的整合提供简单消 ...

  8. 如何使用共享网卡的NAT模式配置VMware12中的CentOS6.7的上网功能

    1.首先共享网卡的NAT模式是通过win10中的VMnet8来通信的,如下双击VMnet8 2.点击[详细信息]查看VMnet8的IPV4地址为192.168.232.110,掩码为255.255.2 ...

  9. python小方法 随笔记

    1. 元组和列表的接收 s1,s2 = [,] print(s1,s2) # 执行结果: 1 2 s3,s4 = (,) print(s3,s4)# 执行结果: 3 4 2. 变量值的交换 a = b ...

  10. 统计学习方法9—EM算法

      EM算法是一种迭代算法,是一种用于计算包含隐变量概率模型的最大似然估计方法,或极大后验概率.EM即expectation maximization,期望最大化算法. 1. 极大似然估计   在概率 ...