213. House Robber II
来源:互联网 发布:知乎 撩妹技巧 编辑:程序博客网 时间:2024/06/05 23:55
Note: This is an extension of House Robber.
After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.
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.
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
这次的房子是围成一圈的。。。
0-1-2-3-4-......-(n-1),然后n-1接到了0
如果第0个房子被抢了,那么只需要考虑编号0——n-2的房子,否则,需要考虑1——n-1的房子
这样就把问题分解成了两个子问题:
class Solution {public: int rob(vector<int>& nums) { int len=nums.size(); if(len==0)return 0; else if(len==1)return nums[0]; return max(rob(nums,0,nums.size()-2),rob(nums,1,nums.size()-1)); } //overload: //use the function in case I int rob(vector<int>& nums,int lo,int hi){ int rob=0,notrob=0; for(int i=lo;i<=hi;i++){ int currob=notrob+nums[i]; notrob=max(notrob,rob); rob=currob; } return max(notrob,rob); }};
- 213.House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II
- 213. House Robber II**
- 213. House Robber II
- 213. House Robber II
- 第一次接触oracle
- 开始写博客
- 模板方法模式
- hadoop学习之MapReduce笔记
- 质数无上界的证明
- 213. House Robber II
- Spring实践之1:开始使用Spring
- 关于Struts2基础案例总是出现404错误的解决方法
- Source Insight基本使用和快捷键
- mysql基础学习(1)
- 将Eclipse中的项目提交到GitHub上
- POJ2352
- leetcode --18. 4Sum
- hiho一下 第163周 希尔伯特曲线