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.
For example:
nums = [2,3,2]
should return 3 because you can't rob house 1 (money = 2) and then rob house 3 (money = 2) because they are adjacent houses.nums = [1,2,3,1]
should return 4 because you rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.nums = [1,2,3]
should return 3.How would you approach this problem?
## Problem Description
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***.
## Naive Solution (Brute Force)
A brute-force solution would involve trying all possible combinations of robbing houses while ensuring no adjacent houses are robbed. This approach has exponential time complexity and is not practical for larger input sizes.
## Optimal Solution (Dynamic Programming)
Since the houses are arranged in a circle, we can consider two cases:
1. Rob the first house and exclude the last house.
2. Don't rob the first house and include the last house.
We can use dynamic programming to solve each case separately and then take the maximum of the two results.
Here's the algorithm:
1. Define a helper function `rob_linear(nums)` that solves the house robber problem for a linear arrangement of houses.
2. Call `rob_linear` twice:
* Once for `nums[1:]` (excluding the first house).
* Once for `nums[:-1]` (excluding the last house).
3. Return the maximum of the two results.
### Python Code
```python
def rob_linear(nums):
if not nums:
return 0
if len(nums) <= 2:
return max(nums)
dp = [0] * len(nums)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i in range(2, len(nums)):
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
return dp[-1]
def rob(nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
return max(rob_linear(nums[1:]), rob_linear(nums[:-1]))
# Example Usage
nums1 = [2, 3, 2]
print(f"Maximum amount for {nums1}: {rob(nums1)}") # Output: 3
nums2 = [1, 2, 3, 1]
print(f"Maximum amount for {nums2}: {rob(nums2)}") # Output: 4
nums3 = [1, 2, 3]
print(f"Maximum amount for {nums3}: {rob(nums3)}") # Output: 3
nums4 = [1]
print(f"Maximum amount for {nums4}: {rob(nums4)}") # Output: 1
nums5 = [1, 2]
print(f"Maximum amount for {nums5}: {rob(nums5)}") # Output: 2
The rob_linear
function iterates through the input array once, so its time complexity is O(n), where n is the number of houses. The rob
function calls rob_linear
twice, so the overall time complexity is still O(n).
The rob_linear
function uses a dp
array of size n to store the maximum amount of money that can be robbed up to each house. Therefore, the space complexity of rob_linear
is O(n). The rob
function calls rob_linear
twice, but the space used by dp
in each call is independent. So, the overall space complexity is O(n).
We can optimize the space complexity to O(1) by using only two variables to store the previous two maximum amounts instead of the entire dp
array. Here's the optimized code:
def rob_linear_optimized(nums):
if not nums:
return 0
if len(nums) <= 2:
return max(nums)
rob1, rob2 = 0, 0
for n in nums:
temp = max(n + rob1, rob2)
rob1 = rob2
rob2 = temp
return rob2
def rob_optimized(nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
return max(rob_linear_optimized(nums[1:]), rob_linear_optimized(nums[:-1]))
# Example Usage with Optimized Solution
nums1 = [2, 3, 2]
print(f"Maximum amount for {nums1} (Optimized): {rob_optimized(nums1)}")
nums2 = [1, 2, 3, 1]
print(f"Maximum amount for {nums2} (Optimized): {rob_optimized(nums2)}")
nums3 = [1, 2, 3]
print(f"Maximum amount for {nums3} (Optimized): {rob_optimized(nums3)}")
With the optimized solution, the space complexity is O(1).
These edge cases are handled in the provided code.