【刷题-LeetCode】198 House Robber
- House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
解1 dfs搜索。超时了。。。
class Solution {
public:
int rob(vector<int>& nums) {
vector<bool>flag(nums.size(), false);
int money = 0, max_money = 0;
dfs(nums, 0, flag, money, max_money);
return max_money;
}
void dfs(vector<int>& nums, int i,
vector<bool>& flag, int &money, int &max_money){
if(i >= nums.size()){
if(money > max_money)max_money = money;
return;
}
if(i > 0){
if(flag[i-1] == false){
flag[i] = true;
money += nums[i];
dfs(nums, i+1, flag, money, max_money);
flag[i] = false;
money -= nums[i];
}
dfs(nums, i+1, flag, money, max_money);
}else{
flag[i] = true;
money += nums[i];
dfs(nums, i+1, flag, money, max_money);
flag[i] = false;
money -= nums[i];
dfs(nums, i+1, flag, money, max_money);
}
}
};
解2 动态规划。dp[k]表示前k家能够抢到的最大金额,对于第k+1家:
- 抢:第k家就不能抢,因此dp[k+1] = dp[k-1] + A[k+1]
- 不抢:dp[k+1] = dp[k]
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size() == 0)return 0;
if(nums.size() == 1)return nums[0];
int dp0 = nums[0], dp1 = max(nums[0], nums[1]);
for(int i = 2; i < nums.size(); ++i){
int tmp = max(dp1, dp0+nums[i]);
dp0 = dp1;
dp1 = tmp;
}
return dp1;
}
};
【刷题-LeetCode】198 House Robber的更多相关文章
- LeetCode刷题------------------------------LeetCode使用介绍
临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...
- 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:二叉树下的不能相邻,求能 ...
- 【刷题-LeetCode】213. House Robber II
House Robber II You are a professional robber planning to rob houses along a street. Each house has ...
- [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 动态规划
题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- (easy)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 ...
随机推荐
- LuoguP7534 [COCI2016-2017#4] Kartomat 题解
Content 火车站里头有一个售票机,其键盘可以看作是一个 \(4\times 8\) 的矩阵,其中第一行的前面三个键和最后一行的最后三个键都是 *,剩余的键按照从上到下,从左到右的顺序依次按照 A ...
- AT266 迷子のCDケース 题解
Content 有 \(n+1\) 个碟,编号为 \(0\sim n\),一开始 \(0\) 号碟在播放机上,其他的碟依次放进了 \(n\) 个盒子里面.现在有 \(m\) 次操作,每次操作找到当前在 ...
- python开发环境软件包安装相关 failed with error code 1 in /tmp/pip-build-vn_f_e1n/psutil/
指定源安装 pip install git+https://github.com/xxxxxx.git pip install -r requirements.txt -i https://mirro ...
- Sort 多列正排序,倒排序
linux sort 多列正排序,倒排序 转自https://segmentfault.com/a/1190000005713784 发布于 2016-06-14 sort是在Linux里非常常用 ...
- JAVA获取请求链接中所有参数(GET请求)
Enumeration<String> paraNames=request.getParameterNames(); for(Enumeration<String> e=par ...
- IDEA启动报错:Error:java: Compilation failed: internal java compiler error
检查是否一致
- 【LeetCode】1413. 逐步求和得到正数的最小值 Minimum Value to Get Positive Step by Step Sum
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 日期 题目地址:https://leetcode ...
- 【九度OJ】题目1109:连通图 解题报告
[九度OJ]题目1109:连通图 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1109 题目描述: 给定一个无向图和其中的 ...
- 【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- .Net下你不得不看的分表分库解决方案-多字段分片
.Net下你不得不看的分表分库解决方案-多字段分片 介绍 本期主角:ShardingCore 一款ef-core下高性能.轻量级针对分表分库读写分离的解决方案,具有零依赖.零学习成本.零业务代码入侵 ...