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.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
  Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
  Total amount you can rob = 2 + 9 + 1 = 12.

题目大意:

数组中每个元素代表一家的财产数量,相邻两家不能都抢,求可抢到的财产的最大数量。

递归解决:

 class Solution {
public: vector<int> result; //消除冗余计算
int solve(vector<int>& nums, int idx) { //当前下标及以前可抢到的最大财产
if (idx < )
return ;
if (result[idx] >= )
return result[idx];
result[idx] = max(nums[idx] + solve(nums, idx - ),
solve(nums, idx - ));
return result[idx];
} int rob(vector<int>& nums) {
result.resize(nums.size(), -);
return solve(nums, nums.size() - );
}
};

迭代解决:

 class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() == )
return ;
vector<int> result(nums.size());
result[] = nums[];
if (nums.size() == )
return nums[];
//result[i]为数组中下标从0到i能抢到的最多财产
result[] = max(nums[], nums[]);
for (int i = ; i < nums.size(); i++)
result[i] = max(result[i - ], nums[i] + result[i - ]);
return result[nums.size() - ];
}
};

或者不用数组:

 class Solution {
public:
int rob(vector<int>& nums) {
if (nums.size() == )
return ;
if (nums.size() == )
return nums[];
int cur, two_back, one_back;
two_back = nums[];
cur = one_back = max(nums[], nums[]);
for (int i = ; i < nums.size(); i++) {
cur = max(nums[i] + two_back, one_back);
two_back = one_back;
one_back = cur;
}
return cur;
}
};

最新文章

  1. android APK更新
  2. linux 分区重新格式化
  3. [Ng]Angular应用点概览
  4. SQL Server 优化器特性导致的内存授予相关BUG
  5. 《软件性能测试与LoadRunner实战教程》新书上市
  6. 《TCP/IP详解 卷一》读书笔记-----广播&amp;多播&amp;IGMP
  7. 自定义继承于Page的基类
  8. Html A标签中 href 和 onclick 同时使用的问题 优先级别
  9. 【Linux安全】安全口令策略设置
  10. leecode 回文字符串加强版
  11. jps(JVM Process Status)
  12. 线程中的异常处理——怪不得所有的语句,都用try catch包的严严实实,甚至每个小步骤还要单独包起来
  13. HDU 3785 寻找大富翁
  14. PIC32MZ Live update bootloader
  15. php简明学习笔记
  16. IIS无法删除应该程序池 因为它包含X个应用程序
  17. 要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10;
  18. Dart学习-操作符
  19. 【python】python打包生成的exe文件运行时提示缺少模块
  20. CentOSmini安装gcc8.2

热门文章

  1. 关于vue2非表单元素使用contenteditable=&quot;true&quot;实现textarea高度自适应
  2. [转] 理解 JavaScript 的 async/await
  3. 学习掌握oracle外表(external table)
  4. Oracle PL/SQL之GROUP BY GROUPING SETS
  5. Android 自动分析apk加固方式
  6. Vue如何封装多个全局过滤器到一个文件
  7. js正则表达式基本语法
  8. pyspark 读写csv、json文件
  9. Spark Shell启动时遇到&lt;console&gt;:14: error: not found: value spark import spark.implicits._ &lt;console&gt;:14: error: not found: value spark import spark.sql错误的解决办法(图文详解)
  10. 1、java线程模型