一道简单的动态规划题目——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 ...
随机推荐
- iOS Ping++前端集成支付
直接上代码 /* 获取订单charge 1.buyerId 买家ID 2.sellerId 卖家ID 3.liveId 直播间ID 4.goodCount 商品数量 5.status ...
- shell常见语法
#!/bin/bashecho "**********argument**********"echo script name: $0echo first argument $1ec ...
- Android Studio使用JNI
0x01 前言 本文讲述使用Android Studio通过静态注册.动态注册使用JNI的方法,以及加载第三方so文件的方法 0x02 Android Studio静态注册的方式使用JNI 1. 添加 ...
- 传统的Ado.net 参数设置:params SqlParameter[] commandParameters
C#代码 ExecuteReader(string connectionString, CommandType commandType, string commandText, params Sql ...
- contentWindow 和contentDocument区别 及iframe访问
a>contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. b>contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 ...
- perl学习笔记(4)——动态加载
在写perl的时候,如果要应用到各种平台的话,比如linux 和windows,会遇到各种问题,有时就是要根据系统类型来加载各种库,之前写的就是这样的, if($^O eq 'linux'){ use ...
- ExtJs内的datefield控件选择日期过后的事件监听select
[摘要]: 选择时间过后我们为什么需要监听事件?一般有这样一种情况,那就是用于比较两个时间大小或者需要判断在哪个时间点上需要做什么样的操作.基于这样的种种情况,我们很有必要琢磨一下datefield控 ...
- IAR EWARM Example Download List
https://srv.iar.com/ExamplesOnDemand/versions.xml http://netstorage.iar.com/SuppDB/Public/EXAMPLES/0 ...
- MON166 User's Guide
MON166 is a debug monitor for C16x and ST10 user programs. It consists of: A configurable monitor pr ...
- 让网站变灰的CSS代码(支持IE、FIREFOX和CHROME)
方法1:支持IE <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xht ...