题目

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: [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: [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.

翻译

你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警

给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。

解题思路

这一题是动态规划问题, 所以首先应该

把问题划分为子问题

分析:

求抢完后的最大金额–>抢到最后一家之后抢到的最大金额,

强盗可能把每一家都当做最后一家抢的店, 所以建立一个数组rob_house_money ,每一项表示当把这一家当做最后抢的店,抢完后获得的最大的钱

rob_house_money [ i ]表示把i当做最后一家要抢的店,抢完后的最大金额

则 最优解可以表示为rob_house_money的最大值

那么rob_house_money的每一个值怎么求呢?因为题目给了要求不能抢相邻的店*, 所以递归式子表示为

for (int j = 0; j < i - 1; j++) {
rob_house_money[i] = max(rob_house_money[i], nums[i] + rob_house_money[j]);
}

前面抢了j店, 然后加上本身店的钱 就是 rob_house_money[i]的值,而且我们都是保留最大的金额, 所以 循环每一个符合条件的店,把最大值赋值给rob_house_money[i]

class Solution {
public:
int rob(vector<int>& nums) {
//异常输出处理
if (nums.size() == 0) {
return 0;
}
//初始化rob_house_money
vector<int> rob_house_money = nums; for (int i = 2; i < nums.size(); i++){
//从第三个房子开始计算, 前面两个房子的值就是本身
for (int j = 0; j < i - 1; j++) {
//i - 1 就略过了相邻的点(前一个店)
rob_house_money[i] = max(rob_house_money[i], nums[i] + rob_house_money[j]);
} }
//返回数组的最大值
return *max_element(rob_house_money.begin(), rob_house_money.end()); }
};

代码优化:

我们发现其实把前面的当做最后一家抢劫的店其实是没有必要的,我们发现rob_house_money[i]的值可以表示为

rob_house_money[i] = max(rob_house_money[i - 1], rob_house_money[i-2] +nums[i])

class Solution {
public:
int rob(vector<int>& nums) { if (nums.size() == 0) {
return 0;
} vector<int> rob_house_money = nums;
//第二个房子需要取 第一个房子和第二个房子的最大值这样才能保持rob_house_money[i-2]是最优的
rob_house_money[1] = max(rob_house_money [0], rob_house_money [1]);
for (int i = 2; i < nums.size(); i++){
rob_house_money[i] = max(rob_house_money[i - 1], rob_house_money[i-2] +nums[i]) ;
} return *max_element(rob_house_money.begin(), rob_house_money.end()); }
};

LeetCode198 House Robber(打家劫舍)的更多相关文章

  1. [LeetCode] House Robber 打家劫舍

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

  2. [LintCode] House Robber 打家劫舍

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

  3. LeetCode198 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. 【LeetCode】198. House Robber 打家劫舍 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...

  6. 198 House Robber 打家劫舍

    你是一个专业的强盗,计划抢劫沿街的房屋.每间房都藏有一定的现金,阻止你抢劫他们的唯一的制约因素就是相邻的房屋有保安系统连接,如果两间相邻的房屋在同一晚上被闯入,它会自动联系警方.给定一个代表每个房屋的 ...

  7. [LeetCode] 213. House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  8. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  9. [LeetCode] 656. Coin Path 硬币路径

    Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...

随机推荐

  1. 评测Loki日志工具

    评测Loki日志工具 目录 评测Loki日志工具 部署Loki 配置grafana 总结: 优势: 劣势: 本文仅对Loki进行简单评测,不涉及原理和细节. 部署Loki Loki是grafana团队 ...

  2. 使用SQL语句进行数据复制

    使用SQL语句对数据或者表进行复制,一般用于两张表结构相同的时候使用. SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; SQL Serve ...

  3. 167两数之和II-输入有序数组

    from typing import List# 这道题很容易能够想到,只需要遍历两边列表就可以了# 两层循环class Solution: def twoSum(self, numbers: Lis ...

  4. 一起学Blazor WebAssembly 开发(1)

    最近blazor的WebAssembly 正式版出来了,正好手头有一个项目采用的前后端分离模式做的,后端用的abp vnext(.net core 的一个很著名的框架)框架开发的,其实前端之前考虑的使 ...

  5. 题解 SP2713 【GSS4 - Can you answer these queries IV】

    用计算器算一算,就可以发现\(10^{18}\)的数,被开方\(6\)次后就变为了\(1\). 所以我们可以直接暴力的进行区间修改,若这个数已经到达\(1\),则以后就不再修改(因为\(1\)开方后还 ...

  6. 剑指offo记录

    一.二维数组中的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是 ...

  7. leetcode题库练习_数组中重复的数字

    题目:数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次 ...

  8. 花了一个月的时间在一个oj网站只刷了这些题,从此入门了绝大多数算法

    如果你想入门算法,那么我这篇文章也许可以帮到你. oj网站有这么多,当然还有其他的.我当初是在hdu上面刷的,不要问我为什么,问就是当时我也是一个新手,懵懵懂懂就刷起来了.点这里可以进入这个网站htt ...

  9. scrapy分布式浅谈+京东示例

    scrapy分布式浅谈+京东示例: 学习目标: 分布式概念与使用场景 浅谈去重 浅谈断点续爬 分布式爬虫编写流程 基于scrapy_redis的分布式爬虫(阳关院务与京东图书案例) 环境准备: 下载r ...

  10. Shell变量的作用域:Shell全局变量、环境变量和局部变量

    Shell 变量的作用域(Scope),就是 Shell 变量的有效范围(可以使用的范围). 在不同的作用域中,同名的变量不会相互干涉,就好像 A 班有个叫小明的同学,B 班也有个叫小明的同学,虽然他 ...