1. 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的更多相关文章

  1. LeetCode刷题------------------------------LeetCode使用介绍

    临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...

  2. 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:二叉树下的不能相邻,求能 ...

  3. 【刷题-LeetCode】213. House Robber II

    House Robber II You are a professional robber planning to rob houses along a street. Each house has ...

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

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

  5. Leetcode 198 House Robber

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

  6. Leetcode 198 House Robber 动态规划

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

  7. Java for LeetCode 198 House Robber

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

  8. (easy)LeetCode 198.House Robber

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

  9. Java [Leetcode 198]House Robber

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

随机推荐

  1. CF330A Cakeminator 题解

    Content 有一个 \(r\) 行 \(c\) 列的矩形蛋糕,由 \(r\times c\) 块 \(1\times 1\) 的蛋糕组成,其中有几块蛋糕上有一些草莓.你不喜欢吃草莓,又想吃得很爽, ...

  2. SP16033 TIPTOP - Tip Top Game 题解

    Description Alim 和 Sufian 是好朋友.他们最近找到了一个好玩的游戏,叫做 Tip Top.游戏规则如下: 确定一个整数. 找出这个整数的所有因子. Alim 先手,每人轮流取一 ...

  3. ts 遇到的问题

    1. Property 'generatePdf' does not exist on type 'Element'. 问题:ts类型校验报错,使得编译失败 解决方法:将报错字段定义为any类型

  4. MySQL 报错:[Err] 1071 - Specified key was too long; max key length is 767 bytes

    [Err] 1071 - Specified key was too long; max key length is 767 bytes 这个会出现在MySQ5.7以下版本 因为没有启用innodb_ ...

  5. SpringBoot内嵌ftp服务

    引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.ftpserver/ftpserver-core --> <depen ...

  6. JAVA在JDK1.8中Stream流的使用

    Stream流的map使用 转换大写 List<String> list3 = Arrays.asList("zhangSan", "liSi", ...

  7. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  8. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  9. 【LeetCode】238. Product of Array Except Self 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 两次遍历 日期 题目地址:https://leetcode.c ...

  10. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...