To solve the House Robber II problem using recursion in C++, we'll need to account for the circular arrangement of the houses. This means that the first and last houses are adjacent, and hence, both cannot be robbed on the same night.
Here's a step-by-step approach to tackle this problem:
-
Understand the Problem Constraints:
- You cannot rob two adjacent houses.
- The houses are arranged in a circle, so the first and last houses are adjacent.
-
Divide the Problem:
- Since the houses are in a circle, we have two scenarios to consider:
- Case 1: Rob houses from the first house to the second last house.
- Case 2: Rob houses from the second house to the last house.
- We'll compute the maximum money that can be robbed in both cases and take the maximum of the two.
-
Implement Recursive Solution with Memoization:
- To avoid redundant computations and optimize the recursive solution, we'll use memoization.
- We'll create a helper function
rob_linear that takes the range of houses to consider and returns the maximum money that can be robbed in that range.
- We'll call this helper function for both cases mentioned above and return the maximum of the two results.
Here's the complete C++ code implementing this approach:
#include <bits/stdc++.h>
using namespace std;
#define int long long
// Recursive function with memoization to find the maximum robbery amount
int rob_linear(const vector<int>& nums, int start, int end, vector<int>& memo) {
if (start > end)
return 0;
if (start == end)
return nums[start];
if (memo[start] != -1)
return memo[start];
// Option 1: Rob the current house and proceed to the house after next
int rob = nums[start] + rob_linear(nums, start + 2, end, memo);
// Option 2: Skip the current house and proceed to the next house
int notrob = rob_linear(nums, start + 1, end, memo);
// Store the maximum of the two options in memo and return
memo[start] = max(rob, notrob);
return memo[start];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// Uncomment the following lines if multiple test cases are needed
// cin >> t;
// cout << t << endl;
while(t--){
int n;
cin >> n;
vector<int> nums(n);
for(int &num : nums)
cin >> num;
if(n == 0){
cout << 0;
return 0;
}
if(n == 1){
cout << nums[0];
return 0;
}
// Case 1: Exclude the last house
vector<int> memo1(n, -1);
int case1 = rob_linear(nums, 0, n - 2, memo1);
// Case 2: Exclude the first house
vector<int> memo2(n, -1);
int case2 = rob_linear(nums, 1, n - 1, memo2);
// The maximum of the two cases is the answer
cout << max(case1, case2);
}
return 0;
}
Explanation of the Code:
-
Function rob_linear:
- Parameters:
nums: The array of money in each house.
start and end: The range of houses to consider.
memo: A memoization array to store intermediate results.
- Base Cases:
- If
start > end, no houses are left to rob, so return 0.
- If
start == end, only one house is left to rob, so return its value.
- Memoization Check:
- If the result for the current
start is already computed (memo[start] != -1), return it to avoid redundant calculations.
- Recursive Cases:
- Rob the Current House: Add the current house's money and proceed to the house after next (
start + 2).
- Skip the Current House: Move to the next house (
start + 1) without adding the current house's money.
- Store and Return Maximum: Store the maximum of the two options in
memo[start] and return it.
-
Main Function Workflow:
- Input Handling:
- Read the number of houses
n and the amount of money in each house.
- Edge Cases:
- If there are no houses (
n == 0), the maximum money is 0.
- If there's only one house (
n == 1), rob that house.
- Compute Maximum for Both Cases:
- Case 1: Exclude the last house. Initialize a memoization array
memo1 with -1 and call rob_linear from house 0 to n-2.
- Case 2: Exclude the first house. Initialize another memoization array
memo2 with -1 and call rob_linear from house 1 to n-1.
- Final Answer: Take the maximum of
case1 and case2 and output it.
Key Points:
-
Handling Circular Houses: By considering two separate cases—excluding the first house or excluding the last house—we effectively break the circular dependency, ensuring that both the first and last houses aren't robbed together.
-
Memoization: This technique significantly optimizes the recursive solution by storing and reusing intermediate results, thereby reducing the time complexity from exponential to linear.
-
Edge Case Considerations: Always handle scenarios with 0 or 1 houses separately to avoid out-of-bound errors.
Example:
Let's consider an example to illustrate how the code works.
Input:
4
2 3 2 3
Explanation:
- Houses are arranged in a circle:
[2, 3, 2, 3]
- Case 1: Exclude the last house. Houses considered:
[2, 3, 2]
- Maximum money can be robbed:
2 + 2 = 4
- Case 2: Exclude the first house. Houses considered:
[3, 2, 3]
- Maximum money can be robbed:
3 + 3 = 6
- Final Answer:
6
Output:
6
This optimized recursive approach ensures that we efficiently compute the maximum money that can be robbed without triggering the security system.