zoukankan      html  css  js  c++  java
  • Leetcode 198 House Robber

    1.题目要求

    You are 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.

    2.分析

          题目要求是求money的最大值,因此,一开始可以想到用贪心算法,将没一间房屋的money和房屋号封装为一个struct,按照money值的大小排序,然后从大到小的挑符合要求的房间,在遍历这个struct数组时,需要判断与当前房间号i的相邻房间i+1和i-1是否已经挑选过,代码如下:

     1 struct myHouse
     2 {
     3     int value;
     4     int number;
     5     bool operator<(myHouse &a)
     6     {
     7         return value > a.value;
     8     }
     9 };
    10 
    11 //bool cmp(myHouse &a,myHouse &b)
    12 //{
    13 //    return a.value < b.value;
    14 //}
    15 
    16 class Solution {
    17 public:
    18     int rob(vector<int> &num) {
    19         vector<myHouse> myVector;
    20         int i,j,k;
    21         int len = num.size();
    22         myHouse temp;
    23         int res = 0;
    24         int res1 = 0;
    25         if (0 == len)
    26         {
    27             return res;
    28         }
    29         for(i=0;i<len;i++)
    30         {
    31             temp.value = num[i];
    32             temp.number = i;
    33             myVector.push_back(temp);
    34         }
    35 
    36         sort(myVector.begin(),myVector.end());
    37 
    38         /*for(i=0;i<len;i++)
    39         {
    40             cout << myVector[i].value << " "<<myVector[i].number<<endl;
    41         }*/
    42 
    43         
    44         set<int> myset;//保存我已经偷盗过的房间号
    45         myset.clear();
    46         int tempNum1,tempNum2;
    47         for(i=0;i<len;i++)
    48         {
    49             tempNum1 = myVector[i].number+1;
    50             tempNum2 = myVector[i].number-1;
    51             if(myset.find(tempNum1) != myset.end() || myset.find(tempNum2) != myset.end())
    52             {
    53                 res1+=myVector[i].value;
    54                 continue;                
    55             }
    56 
    57             res+=myVector[i].value;
    58             myset.insert(myVector[i].number);
    59         }
    60         return res>res1 ? res : res1;
    61 
    62     }
    63 };

    提交代码,发现贪心算法不能通过,比如实例{2,3,2},会得到错误答案。

    因此这个贪心算法不行,得想其他的方法。

    对于求极值的问题,还可以考虑使用动态规划Dynamic Programming来解决问题,我们维护一个一位数组dp,其中dp[i]表示到i位置时不相邻数能形成的最大和,经过分析,我们可以得到递推公式dp[i] = max(num[i] + dp[i - 2], dp[i - 1]),代码如下:

     1 class Solution {
     2 public:
     3     int rob(vector<int> &num) {
     4         
     5         
     6         int len = num.size();
     7         if (num.size() <= 1) 
     8             return num.empty() ? 0 : num[0];
     9         vector<int> dp={num[0],max(num[0],num[1])};
    10         
    11 
    12         for (int i=2;i<len;i++)
    13         {
    14             dp.push_back(max(dp[i-1],dp[i-2]+num[i]));
    15         }
    16 
    17         return dp.back();
    18 
    19     }
    20 };

    提交Accepted。

  • 相关阅读:
    [BZOJ 1698] 荷叶池塘
    [BZOJ 3132] 上帝造题的七分钟
    [JLOI2011] 飞行路线
    [Codeforces Round49F] Session in BSU
    [BZOJ 3036] 绿豆蛙的归宿
    CRC-16校验原理
    ubuntu下mysql的安装与配置
    【OpenCV】边缘检测:Sobel、拉普拉斯算子
    我对sobel算子的理解
    梯度算子(普通的+Robert + sobel + Laplace)
  • 原文地址:https://www.cnblogs.com/LCCRNblog/p/4392098.html
Copyright © 2011-2022 走看看