一、题目

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的更多相关文章

  1. {POJ}{动态规划}{题目列表}

    动态规划与贪心相关: {HDU}{4739}{Zhuge Liang's Mines}{压缩DP} 题意:给定20个点坐标,求最多有多少个不相交(点也不相交)的正方形 思路:背包问题,求出所有的正方形 ...

  2. [SinGuLaRiTy] 动态规划题目复习

    [SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...

  3. 通过一道简单的例题了解Linux内核PWN

    写在前面 这篇文章目的在于简单介绍内核PWN题,揭开内核的神秘面纱.背后的知识点包含Linux驱动和内核源码,学习路线非常陡峭.也就是说,会一道Linux内核PWN需要非常多的铺垫知识,如果要学习可以 ...

  4. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  5. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  6. 又一道简单题&&Ladygod(两道思维水题)

    Ladygod Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  7. CSU 1785: 又一道简单题

    1785: 又一道简单题 Submit Page   Summary   Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 602   ...

  8. QDUOJ 一道简单的数据结构题 栈的使用(括号配对)

    一道简单的数据结构题 发布时间: 2017年6月3日 18:46   最后更新: 2017年6月3日 18:51   时间限制: 1000ms   内存限制: 128M 描述 如果插入“+”和“1”到 ...

  9. Python简单的CTF题目hash碰撞小脚本

    Python简单的CTF题目hash碰撞小脚本 import hashlib for num in range(10000,9999999999): res = hashlib.sha1(str(nu ...

随机推荐

  1. 三种JDBC批量插入编程方法的比较

    JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等. 我用Mysql 5.1.5的JDBC driver 分别对三种比较常用的方法做了测试 方法一,使用PreparedStat ...

  2. C# 递归算法与冒泡

    C# 递归算法求 1,1,2,3,5,8,13···static void Main(string[] args){ int[] cSum = new int[10];for (int i = 0; ...

  3. 一个优秀windows C++程序员的知识体系[转]

    转自:一个优秀windows C++程序员的知识体系 思考一个优秀windows C++ 程序员该有哪些知识,可最终发现什么知识都不能少, 看下图: 除了上面知识,程序员还要不断学习, 保持对新知识的 ...

  4. Spring 常用工具类

    1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...

  5. Jdk和Jre目录和三个lib目录说明----外部扩展jar包servlet,mysql,oracle等

    以下文章转载自a personal blog:For Future,因为昨天下午在cmd模式下编译servlet失败,后来在网上找到这篇文章帮我解决了该问题,我觉得挺值得收藏的,并且这篇文章对&quo ...

  6. Why isn't there a SendThreadMessage function?

    Here's an interesting customer question: Windows has PostMessage and SendMessage. It also has PostTh ...

  7. Segger RTT : Real Time Terminal SRAM 调试解决方法

    http://segger.com/jlink-real-time-terminal.html Real Time Terminal SEGGER's Real Time Terminal (RTT) ...

  8. wpa_supplicant 中的wpa_supplicant.conf

    主要记录下wep加密相关的配置文件的写法. network={ ssid="static-wep-test2" key_mgmt=NONE wep_key0= //密钥索引为2, ...

  9. Codeforces Beta Round #5 E. Bindian Signalizing 并查集

    E. Bindian Signalizing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  10. JLayer初体验。。

    Java最近推出的JLayer虽然和sharped window or translucent window一起有位JavaFX的开发做准备之嫌,但是试了一下还是感觉不错滴.. JLayer 和 gl ...