Skip to main content

House Robber II

Problem

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Solution Approach

Expected Time Complexity: O(n)O(n)

Click - to see solution code
#define f first
#define s second

class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if (n == 1) return nums[0];
vector<pair<int, int>> ans(n);
ans[0].f = nums[0];
ans[1].f = nums[1];
ans[1].s = nums[0];
for (int i = 2; i < n; i++) {
ans[i].f = nums[i] + ans[i - 1].s;
ans[i].s = max(ans[i - 1].f, ans[i - 1].s);
}
auto a = ans[n - 1];
ans[1].s = 0;

for (int i = 2; i < n; i++) {
ans[i].f = nums[i] + ans[i - 1].s;
ans[i].s = max(ans[i - 1].f, ans[i - 1].s);
}
int b = max(ans[n - 1].f, ans[n - 1].s);
if (max(a.f, a.s) == b) return b;
return max(b, a.s);
}
};