一道简单的动态规划题目——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 ...
随机推荐
- js set
function ff(){ var sArray = ['a','b','b','b','b','b','d','b']; var iSet = {}; for(var i=0;i<sArra ...
- js 函数的传值问题
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 在Assertion中获取Response的headers,获取headers中信息,获取body(content)
// get the headers of the requestdef content= messageExchange.getResponseContent()def headers = mes ...
- Java组待开发的任务
周枫: A.将digital,xylinkWeb修改为支持oracle版,并完成测试工作.准备好实施安装的步骤和每步需要的文件,比如发布的项目,tomcat,jdk,memcached,数据库等,在单 ...
- codeforces Gym 100187H H. Mysterious Photos 水题
H. Mysterious Photos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/p ...
- 使用TypeDescriptor给类动态添加Attribute
给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案. 先是有这样的一段解释 Attributes are stat ...
- Android 使用 TableLayout 布局拉伸宽度
转自:http://www.cnblogs.com/ghj1976/archive/2011/04/21/2023850.html 布局文件 <?xml version="1.0&qu ...
- 翻译学python---《Learn Python the hard Way》---第一章 绪论
打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧- 在论坛上看到了国外的一本<Learn Python the hard Way> ...
- window.onload与$.ready的差别
在做图书管理系统的时候.实用到window.onload(){}方法.可是遇到了一个问题.就是怎么都不运行,究竟是为什么呢?愁了半天.后来经师姐指点改用了$.ready(){}. 在我的浅浅的了解中觉 ...
- Understanding page frames and pages
Memory in Linux is organized in the form of pages (typically 4 KB in size). Contiguous linear addres ...