leetcode 198. House Robber (Easy)
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)的更多相关文章
- 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:二叉树下的不能相邻,求能 ...
- (easy)LeetCode 198.House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LeetCode] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Leetcode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java [Leetcode 198]House Robber
题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- [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 ...
- Leetcode 198 House Robber 动态规划
题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...
- leetcode 198 House Robber I
function rob(nums) { if(!nums || nums.length === 0) { return 0; } else if(nums.length < 2){ retur ...
随机推荐
- WebAPI服务端内嵌在CS程序里面
有时候我们不需要将WebAPI发布到iis上运行,需要将webapi内嵌到cs程序内部,随程序一起启动,其实比较简单,需要一个类,如下 public class Startup { public st ...
- c# HttpWebRequest https的一些处理
先看下请求方法 public string Get_Request( string strUrl, CookieContainer _cookie = null, string strHost = & ...
- VC6下 try catch 在release下的杯具(默认情况下,要加上throw语句catch才不会被优化掉)
IDE:VC6 今天遇到一个小问题,把我郁闷了好久,××医生的VulEngine不时在wcsstr处发生crash,加了一番强大的参数检查后,再加上了强大的try catch,其实不是很喜欢用try和 ...
- 窗体图片背景(两种方法:设置Brush.Bitmap指向图片,别的控件也可以这样)
var Bitmap: TBitmap; procedure TForm1.FormCreate(Sender: TObject); begin Bitmap := TBitmap.Creat ...
- Qt学习虚拟机--基于MSYS2-MinGW环境并带有各种开源的软件库!
Qt学习虚拟机--基于MSYS2-MinGW环境并带有各种开源的软件库!虚拟机地址,VM10和以上:http://pan.baidu.com/s/1slcTA49包含两个分卷压缩包,加起来5GB多. ...
- spring boot单元测试之MockMvc
spring单元测试之MockMvc,这个只是模拟,并不是真正的servlet,所以session.servletContext是没法用的. @RunWith(SpringRunner.class) ...
- JAVA8之lambda表达式详解
原文:http://blog.csdn.net/jinzhencs/article/details/50748202 lambda表达式详解 一.问题 1.什么是lambda表达式? 2.lambda ...
- 01-pymysql模块的安装
1.从github 下载pymysql 安装包,下载地址为:https://github.com/PyMySQL/PyMySQL 2.解压下载文件,不用打开python交互界面,cd到解压文件所在目录 ...
- golang开发:类库篇(一) Zap高性能日志类库的使用
为什么要用zap来写日志 原来是写PHP的,一直用的error_log,第一次写Go项目的时候,还真不知道该怎么写日志,后来就按照PHP的写法自己不成规范的捣鼓写.去了新公司之后,发现用的是zap.后 ...
- Hexo+NexT(零):最全Hexo+Next搭建博客教程
快速.简洁且高效的博客框架 有位大神说,喜欢写博客的人的人,折腾博客会经历三个阶段.找到一个免费空间,搭建一个博客,很欣喜,很有成就感,此为一阶段:受限免费空间各种限制,自己买空间和域名,实现对博客的 ...