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. IdHTTP设置SSL证书,乱码问题也解决了

    要跟银行做接口,需要使用delphi来post数据,但对方提供的是https开头的网址,需要使用证书,对方已提供证书了,但是还是调用不成功,使用的是idhttp和TIdSSLIOHandlerSock ...

  2. Java的Qt绑定 jambi

    大二在学java,所以有时会写点java的小程序,可是习惯了qt的界面,使用AWT和swing让我有些不适,后来发现了jambi,才知道原来早就有了java的绑定版,所以迫不及待的安装了上.      ...

  3. 发现 TSplitter 在嵌套时不好用, 索性写了个替代品(处理MouseDown,MouseMove,MouseUp,然后设定控件的Left值就可以了)

    代替 TSplitter 的 TDirPanel 类: unit DirPanel; interface uses   Classes, Controls, Forms, ExtCtrls; type ...

  4. New,Getmem,ReallocMem联系与区别(转)

    procedure New(var P: Pointer);  {为一个指针变量分配内存,会自动计算指针所指数据结构需要空的空间大小} procedure GetMem(var P: Pointer; ...

  5. Windows10 下运行Linux子系统

    关于Windows10 下运行Linux子系统: Windows10内置Linux子系统初体验:http://www.jianshu.com/p/bc38ed12da1d Win10运行Ubuntu版 ...

  6. python trojan development 2nd —— use python to send mail and listen to the key board then combine them

    请勿用于非法用途!!!!!本人概不负责!!!原创作品,转载说明出处!!!!! from pynput.keyboard import Key,Listener import logging impor ...

  7. HBase 学习之路(五)——HBase常用 Shell 命令

    一.基本命令 打开Hbase Shell: # hbase shell 1.1 获取帮助 # 获取帮助 help # 获取命令的详细信息 help 'status' 1.2 查看服务器状态 statu ...

  8. java集合知识点总结

    下面是java中常见的集合: List--列表:内部元素有序,可以重复, ArrayList:线程不安全,效率高.数据结构是线性表,底层结构是顺序表,也就是数组,有唯一的下标来指定元素的位置,查询快, ...

  9. JavaWeb入门_模仿天猫整站Tmall_SSM实践项目

    Tmall_SSM 技术栈 Spring MVC+ Mybatis + Spring + Jsp + Tomcat , 是 Java Web 入门非常好的练手项目 效果展示: 模仿天猫前台 模仿天猫后 ...

  10. 【python3两小时快速入门】入门笔记01:基础

    又要我搞爬虫了,这次的源网站使用的ajax加载数据,我用java爬下来的页面内容部分全都是空,虽然java也有插件,但是使用起来感觉很麻烦,所以,python!老子来了.    1. 版本:pytho ...