Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
House Robber in Python
The House Robber problem is a classic dynamic programming challenge. A robber wants to rob houses along a street, but cannot rob two adjacent houses as this would trigger an alarm. We need to find the maximum amount that can be robbed.
Given an array where each element represents the amount of money in each house, we must determine the maximum sum possible without selecting adjacent elements.
Problem Understanding
For the array [2, 7, 10, 3, 1], the optimal strategy is to rob houses at indices 0, 2, and 4 (values 2, 10, 1) for a total of 13. We skip adjacent houses to avoid triggering the security system.
Algorithm Approach
We use dynamic programming with two variables ?
-
prev1? maximum amount robbed including the current house -
prev2? maximum amount robbed excluding the current house
For each house, we decide whether to rob it or skip it based on which gives the maximum amount.
Implementation
def house_robber(nums):
"""
Find maximum amount that can be robbed without robbing adjacent houses
"""
if not nums:
return 0
prev2 = 0 # Max amount excluding previous house
prev1 = 0 # Max amount including previous house
for amount in nums:
temp = prev1
prev1 = max(prev2 + amount, prev1) # Rob current or skip
prev2 = temp
return prev1
# Test with the example
houses = [2, 7, 10, 3, 1]
result = house_robber(houses)
print(f"Houses: {houses}")
print(f"Maximum amount: {result}")
Houses: [2, 7, 10, 3, 1] Maximum amount: 13
Step-by-Step Execution
def house_robber_detailed(nums):
prev2, prev1 = 0, 0
for i, amount in enumerate(nums):
temp = prev1
prev1 = max(prev2 + amount, prev1)
prev2 = temp
print(f"House {i}: amount={amount}, prev1={prev1}, prev2={prev2}")
return prev1
houses = [2, 7, 10, 3, 1]
result = house_robber_detailed(houses)
print(f"Final result: {result}")
House 0: amount=2, prev1=2, prev2=0 House 1: amount=7, prev1=7, prev2=2 House 2: amount=10, prev1=12, prev2=7 House 3: amount=3, prev1=12, prev2=12 House 4: amount=1, prev1=13, prev2=12 Final result: 13
Alternative Solutions
Using Array-based DP
def house_robber_dp_array(nums):
if not nums:
return 0
if len(nums) == 1:
return nums[0]
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]
houses = [2, 7, 10, 3, 1]
result = house_robber_dp_array(houses)
print(f"Result using DP array: {result}")
Result using DP array: 13
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Two Variables | O(n) | O(1) | Space-efficient solution |
| DP Array | O(n) | O(n) | Understanding the logic |
Conclusion
The House Robber problem demonstrates dynamic programming where we make optimal decisions at each step. The two-variable approach provides an O(1) space solution while maintaining O(n) time complexity.
