一道简单的动态规划题目——House Robber
一、题目
House Robber(一道Leetcode上的关于动态规划的简单题目)具体描述如下:
There is 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.
二、题意理解:
1. 题目描述的意思是假设有一位专业的小偷要对街上一排互相相邻的房间实施偷盗,但没两个相邻的房间之间有安保措施,所以不能对两个相邻的房间同时实施偷盗,不然就会触发报警装置。给定一个数组列表,每个元素代表每间房子中的money的数目,题目要求在不触发警报的前提下,该小偷一次最多能偷多少money?
2. 这是一道典型的动态规划类型的题目,小偷在一次偷盗过程中有多种实施方案,每个方案的结果(偷得的money数目)不一定一样,目的就是要求出能得到最大数目的方案。假设给定的数组列表如下:

可以看到总共有10间房子,并且其中每间房子的money数量用黑色字体的数字标示。
3. 算法思路:
3.1 假设小偷偷得顺序是按照从左往右,那么最终停止的位置只能是9或10
3.2 如果从位置10往前回溯,分别有两个可以选择的房子7和8,位置9也是一样的
3.3 需要选择从左边开始到money数目最大的那个房子,那么可以看到这是一个递归的过程
3.4 因为中间会有一些重复的计算,比如在求位置10的向前回溯的时候,需要计算位置7的money值,计算位置9前溯同样需要计算位置7的money,所以我们需要将已经计算过的值进行记录
三、程序实例
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
int rob(vector<int>& nums)
{
int len = nums.size();
maxRob.resize(len, -1);
int m1 = robRec(nums, len-1);
int m2 = robRec(nums, len-2);
return m1 > m2?m1:m2;
}
int robRec(vector<int>&nums, int pos)
{
if(pos < 0)
return 0;
if(maxRob[pos] >= 0)//判断是否已经计算过当前位置的值
return maxRob[pos];
int max1 = robRec(nums, pos-2);
int max2 = robRec(nums, pos-3);
maxRob[pos] =(max1 > max2?max1:max2) + nums[pos];
return maxRob[pos];
}
private:
vector<int> maxRob;
};
int main()
{
int arr[] = {2,3,4,1,9 ,3 ,2, 3, 3 ,4};
Solution so;
vector<int> int_vec(arr, arr+sizeof(arr)/sizeof(int));
cout << so.rob(int_vec);
return 0;
}
一道简单的动态规划题目——House Robber的更多相关文章
- {POJ}{动态规划}{题目列表}
动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...
- [SinGuLaRiTy] 动态规划题目复习
[SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...
- 通过一道简单的例题了解Linux内核PWN
写在前面 这篇文章目的在于简单介绍内核PWN题,揭开内核的神秘面纱.背后的知识点包含Linux驱动和内核源码,学习路线非常陡峭.也就是说,会一道Linux内核PWN需要非常多的铺垫知识,如果要学习可以 ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- POJ 动态规划题目列表
]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...
- 又一道简单题&&Ladygod(两道思维水题)
Ladygod Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit S ...
- CSU 1785: 又一道简单题
1785: 又一道简单题 Submit Page Summary Time Limit: 5 Sec Memory Limit: 128 Mb Submitted: 602 ...
- QDUOJ 一道简单的数据结构题 栈的使用(括号配对)
一道简单的数据结构题 发布时间: 2017年6月3日 18:46 最后更新: 2017年6月3日 18:51 时间限制: 1000ms 内存限制: 128M 描述 如果插入“+”和“1”到 ...
- Python简单的CTF题目hash碰撞小脚本
Python简单的CTF题目hash碰撞小脚本 import hashlib for num in range(10000,9999999999): res = hashlib.sha1(str(nu ...
随机推荐
- How Tomcat Works(十八)
在前面的文章中,如果我们要启动tomcat容器,我们需要使用Bootstrap类来实例化连接器.servlet容器.Wrapper实例和其他组件,然后调用各个对象的set方法将它们关联起来:这种配置应 ...
- 安装、设置与启动MySql5.1.30绿色版的方法
1.解压 mysql-noinstall-5.1.30-win32.zip(下载地址http://dev.mysql.com/downloads/mysql/5.1.html) 2.在 F 盘建立目录 ...
- DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化
http://www.360doc.com/content/13/0712/09/10504424_299336674.shtml Linq处理List数据 http://blog.163.com/l ...
- Builder
Builder模式的使用情景 相同的方法, 不同的执行顺序, 产生不同的事件结果 多个部件或零件, 都可以装配到一个对象中, 但是产生的运行结果又不相同 产品类比较复杂, 或者产品类中的调用顺序不同产 ...
- arm_cm4.c关于kinetis的修改
/***********************************************************************/ /* * Initialize the NVIC t ...
- C++ assert()断言
assert是一个宏定义,原型定义在<assert.h>中: #include <assert.h> void assert( int expression ); 其作用是:如 ...
- Windows编写bat执行文件
1:建立TXT文件 Rem nping用来测试IP地址的连通性 Rem nping --tcp -p 80 --flags rst --ttl 2 192.168.1.1 date 2:重命名为bat ...
- 05_android入门_GET方式实现登陆(在控件上显示服务端返回的内容)
当点击登陆之后,怎么把server端返回的数据,写到指定的控件上尼?,在android怎么实现尼?以下我们通过详细的代码进行分析和实现,希望能对你,在学习android知识上有所帮助. 以下通过代码说 ...
- Failed to load resource: net::ERR_CACHE_MISS
Failed to load resource: net::ERR_CACHE_MISS 译为开发人员工具载入缓存的时候,说找不到资源. 原因是你先打开页面,然后打开chrome的开发人员工具.而页面 ...
- GLSL实现HDR Rendering 【转】
http://blog.csdn.net/a3070173/archive/2008/11/29/3408573.aspx HDR - 全称High dynamic rang,是目前流行的3D特效技术 ...