Note: This is a extension of house robber.
After robbing those houses on that street, the thief have found himself a new place for his thievery so that he would not GE T too much attention. This time, all houses at the is arranged in a circle. That's means the first house was 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 in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.
Basic ideas:
Decompose this problem into two sub-problems.
The total number of rooms is n.
1. Rob the 1th House. At this time, the last room can not be robbed. The available range is [0, n-1]
2. Do not rob the 1th house. At this time, the last room can be robbed. The available range is [1, n]
Class Solution {public: int Rob (vector<int>& nums) { if (nums.size () = = 1) return nums[0]; Return Max (Rob (nums, 0, Nums.size ()-1), Rob (Nums, 1, Nums.size ())); } int Rob (vector<int>& nums, int start, int stop) { int last_last = 0, last = 0; for (int i=start; i<stop; i++) { int temp = MAX (Last_last+nums[i], last); Last_last = last; last = temp; } return last; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
House robber II--Leetcode