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. 【LeetCode】455. Assign Cookies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  2. 【LeetCode】528. Random Pick with Weight 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-pi ...

  3. iNeuOS工业互联网操作系统,矿山动态产量计量系统和铁路车辆识别系统应用场景案例

    目       录 1.      概述... 2 2.      平台演示... 2 3.      矿山动态产量计量系统... 2 4.      铁路车辆识别系统... 4 1.   概述 iN ...

  4. 解决ubuntu突然无法联网问题

    一.问题描述 今天使用笔记本远程办公的时候,突然电脑无法联网了,使用chrome浏览器访问网页出现如下错误 This site can't be reachedwww.baidu.com's serv ...

  5. Variational Autoencoders and Nonlinear ICA: A Unifying Framework

    目录 概 主要内容 本文的模型 Identifiability Khemakhem I., Kingma D. P., Monti R. P. and Hyv"{a}rinen A. Var ...

  6. Ubuntu复习笔记-认识Linux

    本次复习基于\(Ubuntu20.04\)的发行版进行总结,目的是更好记录自己学习的\(Linux\). 认识Linux 学习\(Linux\)之前,需要搞懂几个概念,\(Linux\)桌面操作系统与 ...

  7. 【MySQL作业】MySQL函数——美和易思日期和时间函数应用习题

    点击打开所使用到的数据库>>> 1.采用尽可能多的方式显示当前系统日期和时间. 下列 SQL 语句可以显示当前系统的日期和时间: curdate() 和 current_date() ...

  8. Java高级程序设计笔记 • 【第2章 多线程(一)】

    全部章节   >>>> 本章目录 2.1 线程的概述 2.1.1 进程 2.1.2 多线程优势 2.1.3 Thread 类 2.1.4 实践练习 2.2 Runnable接口 ...

  9. 编写Java程序,将一个int型数组拼接成字符串

    返回本章节 返回作业目录 需求说明: 将一个int数组中的元素拼接成int元素以逗号分隔字符串. 实现思路: 定义一个数组变量int[] arrs = {12,21,33,9,2}. 定义一个方法ar ...

  10. 射频FEM介绍

    FEM介绍 1. 什么是FEM 1.1 FEM简介 FEM,Front-end Modules,即就是前端模块.硬件电路中的前端模块完成射频信号的发送放大以及接收放大(with bypass).滤波, ...