leetcode——213——House Robber II
2016-04-22 12:14
323 查看
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.
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.
class Solution { public: int rob(vector<int> &nums) { if (nums.size() == 0) return 0; if (nums.size() == 1) return nums[0]; return max(robber(nums, 0, nums.size()-1), robber(nums, 1, nums.size())); } int robber(vector<int> &num,int start,int end) { int best0 = 0; // 表示没有选择当前houses int best1 = 0; // 表示选择了当前houses for(int i = start; i < end; i++){ int m = max(best1,best0+num[i]); best0 = best1; best1 = m; } return best1; } };
相关文章推荐
- Pictures
- SGU 107 987654321 problem【找规律】
- android中sqlite数据库升级
- SGU 107 987654321 problem【找规律】
- 数据库良好的设计
- NOIP2015 PJ T3,T4 题解
- Android WebView 远程执行代码漏洞浅析
- 单列索引和多列索引性能对比,索引字段空值情况的处理
- NSCache的简单介绍应用
- 如何获得Windows的启动时间
- 逻辑运算符 && ! 的具体运用——判断一个数是不是两位数(这个数前提必须是 整数 正数)
- [wxWidgets]_[0基础]_[不常见但有用的类wxStandardPaths]
- 佛主保佑,永无BUG
- surfaceview图像拉伸问题
- 结构体写法用于set map暂时乱存一下
- Linux基础入门(1):用户及文件权限管理
- PCM音频文件的制作
- 浅谈(随机)测试注意事项三
- UML基础
- 操作系统作业调度--操作系统