LeetCode198 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: [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(打家劫舍)的更多相关文章
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [LintCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- LeetCode198 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 打家劫舍 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...
- 198 House Robber 打家劫舍
你是一个专业的强盗,计划抢劫沿街的房屋.每间房都藏有一定的现金,阻止你抢劫他们的唯一的制约因素就是相邻的房屋有保安系统连接,如果两间相邻的房屋在同一晚上被闯入,它会自动联系警方.给定一个代表每个房屋的 ...
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [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 ...
- [LeetCode] 656. Coin Path 硬币路径
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
随机推荐
- Cyber Security - Palo Alto Security Policies(2)
Task 3 The SOC(Security Operation Center) monitoring team dashboard reported more 1,000 requests to ...
- Vue全家桶之一Vue(基础知识篇)
全家桶:Vue本身.状态管理.路由. 异步组件:
- Fisher算法+两类问题
文章目录 一.Fisher算法 二.蠓的分类问题: 三.代码实现: 一.Fisher算法 二.蠓的分类问题: 两种蠓Af和Apf已由生物学家根据它们的触角和翼长加以区分(Af是能传播花粉的益虫,Apf ...
- webpack 编译时,提示 Unexpected token: keyword «const»
代码里如果用到const 关键字,编译报这种错误 解决方法: npm install terser-webpack-plugin --save 然后,webpack配置: const TerserPl ...
- 2. 妈呀,Jackson原来是这样写JSON的
没有人永远18岁,但永远有人18岁.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习.关注公众 ...
- hadoop2.7.3+spark2.0.1+scala2.11.8集群部署
一.环境 4.用户 hadoop 5.目录规划 /home/hadoop/app #程序目录 /home/hadoop/data #数据目录 #打开文件的最大数 vi /etc/sec ...
- Window版本的Python安装库大全
1. 位置 python的pip安装包网站 https://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载方法 wget https://download.lfd.uci ...
- 以细胞为例 说一下dfs和bfs的思路
今天发现很少写dfs.. dfs主要思想是递归 bfs主要靠队列 先说一下这个题我被阻了半个小时的地方: 1读数一定要注意scanf的吃回车 2注意数据类型为char,判断时是'0' dfs: #in ...
- PHP is_executable() 函数
定义和用法 is_executable() 函数检查指定的文件是否可执行. 如果文件可执行,该函数返回 TRUE. 语法 is_executable(file) 参数 描述 file 必需.规定要检查 ...
- PDO::rollBack
PDO::rollBack — 回滚一个事务(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0) 说明 语法 bool PDO::rollBack ( void )高佣联 ...