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,唯一的區(qū)別在于房屋排列是環(huán)形的,而非線性,這樣頭和尾的房屋是不能同時被偷竊的。因此在原有的基礎上增加一個狀態(tài),即第一間房屋是否被偷竊。
采用兩位二進制數(shù)表示偷竊到第i家時的狀態(tài),高位表示是否在第1家偷竊,低位表示是否在第i家偷竊。比如:
00 | 01 |
---|---|
不在第1家偷竊,也不在第i家偷竊 | 不在第1家偷竊,但在第i家偷竊 |
10 | 11 |
在第1家偷竊,但不在第i家偷竊 | 在第1家偷竊,也在第i家偷竊 |
由此,到達第i家時的狀態(tài)如下:
robMoney[i][0] = max(robMoney[i-1][0], robMoney[i-1][1]);
robMoney[i][1] = robMoney[i-1][0] + nums[i];
robMoney[i][2] = max(robMoney[i-1][2], robMoney[i-1][3]);
robMoney[i][3] = robMoney[i-1][2] + nums[i];
注意
- 應特別注意的是,最后一家只能有狀態(tài)
00, 01, 10
。 - 同時,應特殊處理只有1家的情況,此時
11
才是正確的狀態(tài)。 - 第1家時,
01, 10
是矛盾的狀態(tài),robMoney
設為MIN_INT
。
AC代碼
class Solution {
public:
const static int MIN_INT = -999999999;
int rob(vector<int>& nums) {
int houseNumber = nums.size();
if (!houseNumber) return 0;
if (houseNumber == 1) return nums[0];
int robMoney[houseNumber][4];
for (int i = 0; i != houseNumber; ++i) {
for (int j = 0; j != 4; ++j) {
robMoney[i][j] = 0;
}
}
robMoney[0][3] = nums[0];
robMoney[0][1] = robMoney[0][2] = MIN_INT;
for (int i = 1; i < houseNumber; ++i) {
robMoney[i][0] = max(robMoney[i-1][0], robMoney[i-1][1]);
robMoney[i][1] = robMoney[i-1][0] + nums[i];
robMoney[i][2] = max(robMoney[i-1][2], robMoney[i-1][3]);
robMoney[i][3] = robMoney[i-1][2] + nums[i];
}
return max(robMoney[houseNumber-1][0], robMoney[houseNumber-1][1], robMoney[houseNumber-1][2]);
}
int max(int x, int y) { return x > y ? x : y; }
int max(int x, int y, int z) {
if (x >= y && x >= z) return x;
if (y >= x && y >= z) return y;
return z;
}
};