Watch 10 video solutions for House Robber, a medium level problem involving Array, Dynamic Programming. This walkthrough by take U forward has 505,828 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems 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.
Example 1:
Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 400Problem Overview: You are given an array where each element represents money stored in a house along a street. Adjacent houses cannot be robbed on the same night because they trigger an alarm. The goal is to compute the maximum amount of money you can steal without robbing two neighboring houses.
Approach 1: Dynamic Programming - Iterative DP Array (O(n) time, O(n) space)
This problem fits the classic dynamic programming pattern where each decision depends on previous optimal results. Define dp[i] as the maximum money you can rob from the first i houses. For each house, you either rob it or skip it. If you rob house i, you add its value to dp[i-2]. If you skip it, you keep dp[i-1]. The recurrence becomes dp[i] = max(dp[i-1], dp[i-2] + nums[i]). Iterate through the array, computing the best outcome at each step.
This approach explicitly stores results for every house, making the logic easy to visualize and debug. It clearly shows how the problem reduces to choosing between two previous states. The tradeoff is extra memory proportional to the number of houses.
Approach 2: Dynamic Programming - Optimized Space (O(n) time, O(1) space)
The DP recurrence only depends on the previous two states: dp[i-1] and dp[i-2]. Instead of maintaining a full array, keep two variables representing these values. As you iterate through the houses, compute the current best value and shift the variables forward.
At each step, calculate current = max(prev1, prev2 + num), where prev1 stores the best result up to the previous house and prev2 stores the best result two houses back. Update the variables as you move forward. This keeps the same decision logic while eliminating the DP array.
This version is the standard optimal solution discussed in interviews. It still performs a single pass through the array and maintains constant memory usage. The algorithm behaves like a rolling dynamic programming window that tracks only the necessary states.
Recommended for interviews: Start by explaining the DP recurrence using the array-based solution since it clearly demonstrates the decision process between robbing and skipping a house. Then optimize it to the constant-space version. Interviewers usually expect the O(n) time, O(1) space solution because it shows strong understanding of dynamic programming state transitions and space optimization.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming - Iterative DP Array | O(n) | O(n) | Best for understanding the DP recurrence and explaining the transition clearly in interviews |
| Dynamic Programming - Optimized Space | O(n) | O(1) | Preferred in production or interviews when memory optimization is expected |