一道简单的动态规划题目——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;
}

                        

转载于:https://www.cnblogs.com/mjbjtunlp/p/5930553.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值