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
House Robber Problem OverviewCannot rob adjacent houses - maximize total moneyHouses:ROB2SKIP7ROB9SKIP3ROB1House 0House 1House 2House 3House 4Red X marks show adjacent constraintGreen houses can be robbed: 2 + 9 + 1 = 12Maximum Money: 12
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)
Asked in
Amazon 45 Google 38 Microsoft 32 Facebook 28
67.9K Views
High Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen