House Robber - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,7,9,3,1]
›
Output:
12
💡 Note:
Rob houses 0, 2, and 4: 2 + 9 + 1 = 12. Cannot rob adjacent houses 1 and 3.
Example 2 — Choose Larger Adjacent
$
Input:
nums = [1,2,3,1]
›
Output:
4
💡 Note:
Rob houses 1 and 3: 2 + 3 = 5, but better to rob houses 0 and 2: 1 + 3 = 4. Actually optimal is 2 + 1 = 4 (houses 1,3).
Example 3 — Single House
$
Input:
nums = [5]
›
Output:
5
💡 Note:
Only one house available, rob it for maximum of 5.
Constraints
- 1 ≤ nums.length ≤ 100
- 0 ≤ nums[i] ≤ 400
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of house values with adjacency constraint
2
Process
Choose non-adjacent houses for maximum sum
3
Output
Maximum money that can be robbed
Key Takeaway
🎯 Key Insight: At each house, choose max of (rob current + best two houses ago) vs (skip current, keep previous best)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code