您的位置:首页 > 其它

[leetcode] House Robber II

2015-06-13 10:45 399 查看
From : https://leetcode.com/problems/house-robber-ii/
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.
class Solution {
public:
int find(vector<int>& nums, int start, int end) {
int n1=nums[start], n2=0, t;
for(int i=start+1; i<=end; i++) {
t = n1;
n1 = max(n1, n2+nums[i]);
n2 = t;
}
return n1;
}
int rob(vector<int>& nums) {
if(nums.size() == 0) return 0;
if(nums.size() == 1) return nums[0];
return max(find(nums, 0, nums.size()-2), find(nums, 1, nums.size()-1));
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: