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 ...
随机推荐
- OSCP Learning Notes - Buffer Overflows(5)
Generating Shellcode & Gaining Root 1.Generate the shellcode on Kali Linux. LHOST is the IP of K ...
- Ethical Hacking - NETWORK PENETRATION TESTING(16)
ARP Poisoning - MITMf MITMf is a framework that allows us to launch a number of MITM attacks. MITMf ...
- idea 导入eclipse play1.2.7项目
1.play eclipsify #myapp 转为eclipse目录结构 2.导入eclipse,一路next. 3.新增个Application -Xms1536m-Xmx2048m-Xdebug ...
- HBase面试考点
HBase 架构图 组成部分及作用 Zookeeper在HBase中作用 Master的高可用 RegionServer的监控 元数据的入口 HMaster 不仅有维护集群元数据信息的功能,还能 通过 ...
- hcharts生成图表
借助hcharts插件,可以很方便地在模板页面中生成图表.类似插件还有echarts. 补充...
- Spring boot 基础整理(一)
环境准备 (1)JDK 环境必须是 1.8 及以上(2)后面要使用到 Maven 管理工具 3.2.5 及以上版本,所以会先介绍 Maven 的安装与配置(3)开发工具建议使用 IDEA,也可以 My ...
- 预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值
PHP $_GET 变量 在 PHP 中,预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值. $_GET 变量 预定义的 $_GET 变量用于收集来自 ...
- PHP array_udiff_uassoc() 函数
实例 比较两个数组的键名和键值(使用用户自定义函数进行比较),并返回差集: <?phpfunction myfunction_key($a,$b){if ($a===$b){return 0;} ...
- 5.22 noip模拟赛
本来我是不想写的,无奈不会写.蒟蒻 考场就是想不出来 今天得到了100分额外水过了100分我是真的失败.还有一个根本不会check 感觉自己非常之菜. 这道题是这样的 还行吧比较有意思 首先确立一个真 ...
- KMP,HASH,Trie,AC自动机
我做个总结算了下午看了一下AC自动机和学习我的大生物(当然是多谢鑫神了)..完了要崩.. 1 KMP 只要是学过的人都觉得比较简单吧 但是学不会的人就感觉很难了,我是那种顿悟的然后感觉非常简单的人过程 ...