问题描述:

意思就是说:给定一个数组,寻找一种选取方法,使得在保证任何两个相邻元素不同时被选的条件下得到的数值总和最大。

1. 递归

  设nums为数组首地址,numsSize为数组的大小,max[i]表示从第i(0 <= i < numsSize)个元素开始所能得到的最大值。则原问题描述为求max[0]。

  

  其中nums[i]+max[i+2]表示选择第i个元素,因此第i+1个元素不能选择;max[i+1]表示不选择第i个元素,而max[i+1]=max{nums[i+1]+max[i+3], max[i+2]},很明显max[i+2]小于nums[i]+max[i+2],因此:

  

  加上边界控制语句,代码如下:

 int maxMoney ( int *nums, int numsSize, int startIndex) {
     if ( startIndex >= numsSize ) {
         ;
     } ) {
         return *(nums+startIndex);
     }
     );
     ) + maxMoney(nums, numsSize, startIndex+);
     return first>second?first:second;
 }

 int rob(int* nums, int numsSize) {
     ) {
         ;
     }  ) {
         return *nums;
     }  ) {
         )? *nums : *(nums+);
     }
     );
     ) + maxMoney(nums, numsSize, );
     return first>second?first:second;
 }

2. 迭代

  当i=numsSize-1(表示只剩下最后一个元素的时候),最大的只即为nums[i];

  当i=numsSzie-2(剩下最后两个元素的时候,下文其它情况依次类推),最大的情况是nums[i](选择numsSize-2)或nums[i+1](选择numsSize-1);而nums[i+1]已经在i=numsSize-1的时候进行了分析,因此不用再重复;

  当i=numsSize-3时,最大值只可能是

    1. nums[i]+nums[i+2],或者
    2. nums[i+1];

  而nums[i+1](即nums[numsSize-2])已经在第二步计算过,因此只需要记录nums[i]+nums[i+2]的值;

  ……

  下表是一个简单的分析过程,一些不可能出现最大值的情况没有写出来。

  通过上述分析可知,求max[i]时,不选择第i个元素的情况(即max[i+1])已经计算过,因此只需要增加选择第i个元素的情况,那么最终的结果就是这两个值中较大的。

  而对于选择i的情况,第i+1元素由于限制条件将不能选择,因此第i+2个元素可以选可以不选:

    1. 如果选择第i+2个元素,则max[i]=nums[i]+max[i+2];
    2. 如果不选择第i+2个元素,则i+3个元素将必须选择(因为如果不选,则将出现连续3个元素没有选择的情况,这样无论如何都不可能得到一个整体最优的值),则max[i]=nums[i]+max[i+3]

  由于max[i+2]和max[i+3]已经计算出来,因此选择其中一个最大的值作为max[i]的结果。

  这样可以计算出max中所有元素的值,但所有的值都是选择当前假设条件下第一个元素的情况,不选择当前元素的情况即为前一次计算的结果。最终的结果即为max[0]和max[1]中较大的。

 int rob(int* nums, int numsSize) {
      ) {;}
      ) {];}

     , temp2 = ;
      ) {
         temp1 = nums[];
         temp2 = nums[];
         return temp1 > temp2 ? temp1 : temp2;
     } ) {
         temp1 = nums[] + nums[];
         temp2 = nums[];
         return temp1 > temp2 ? temp1 : temp2;
     } else {
         // maxValue[i] 表示选择第i个的最大值
         int *maxValue = (int*)malloc(numsSize * sizeof(int));
         maxValue[numsSize-] = nums[numsSize-];
         maxValue[numsSize-] = nums[numsSize-];
         maxValue[numsSize-] = nums[numsSize-] + nums[numsSize-];
         ; i >= ; i--) {
             temp1 = nums[i] + maxValue[i+];
             temp2 = nums[i] + maxValue[i+];
             maxValue[i] = temp1 > temp2 ? temp1 : temp2;
         }
         // maxValue[0]对应第一个(下标为0)选择的最大值,maxValue[1]对应第1个不选择的最大值
         ] > maxValue[] ? maxValue[] : maxValue[];
     }
 }

LeetCode Day5——House Robber的更多相关文章

  1. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  2. [LeetCode] 213. House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  3. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  4. leetcode:House Robber(动态规划dp1)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  5. [LeetCode] 337. House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  6. [LeetCode] 213. House Robber II 打家劫舍之二

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  7. [LeetCode] 198. House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. 【leetcode】House Robber

    题目简述 You are a professional robber planning to rob houses along a street. Each house has a certain a ...

  9. Leetcode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

随机推荐

  1. Elasticlunr.js 简单介绍

    Elasticlunr.js 项目地址:http://elasticlunr.com/ 代码地址:https://github.com/weixsong/elasticlunr.js 文档地址:htt ...

  2. html image -- data:image/png;base64

    1,  data:image/png;base64 <!DOCTYPE HTML> <html> <head> <meta http-equiv=" ...

  3. 放弃使用jQuery实现动画

    在Web开发的圈子里,开发人员经常觉得CSS动画是一种高性能web动画技术.假设想让网页载入的更快一些,就应该用纯CSS动画.事实上这样的观点是错误的,非常多开发人员早就放弃了javascript的动 ...

  4. windows 下使clion支持c++11操作记录

    最近用上了windows下的clion,发现默认安装的MINGW版本太低,导致所带的gcc版本竟然是3.5的,实在太老了,不支持c++11,于是手动修改了mingw的版本.首先去mingw的官网下载最 ...

  5. cookie 和 session 基本使用 以及 封装

    Cookie: 是一小段文本信息,用户请求页面的时候,在浏览器和服务器之间传递.用户每次访问的时候都会记录cookie,cookie里可以包含用户信息,浏览的历史记录等等:Cookie是由服务器端生成 ...

  6. angular $location常用方法使用

    $location提供了一些常用的操作和获取地址栏里的地址的方法. <script type="text/javascript"> angular.module('ap ...

  7. CSS学习笔记——CSS中定位的浮动float

    昨天在解决了盒模型的问题之后又出现了新的知识模糊点:浮动和绝对定位?今天先解决浮动相关的问题,首先列举出想要解决的问题: 1.浮动到底是怎么样的? 2.浮动对元素的影响有什么? 3.浮动主要用来干什么 ...

  8. Information seeking letter, hard copy version

    23 Roanoke Street Blacksburg, VA 24060 (540) 555-1123 K.Walker@vt.edu October 23, 20XY Mr. James G. ...

  9. Core Bluetooth下实现两个设备进行互联

    一.外设管理者 - 发布广告 如果两部手机在BLE的基础上进行连接,需要让其中一部手机作为外设,外设需要进行广播自己需要发布的数据,以供中心设备的接收和处理. 实现外设广播数据并且处理发送过程当中的流 ...

  10. JS 中刷新页面的方法

    整理了就是这几种,,有些在IE下面是不支持的,慎用... 1,history.go(0) 2,location.reload() 3,location=location 4,location.assi ...