mediumDynamic ProgrammingDynamic Programming

House Robber

mediumTime: O(n)Space: O(1)

Recognize the pattern

maximize valuecan't pick adjacent elementslinear sequence of choices

Brute force idea

If you approach House Robber in the most literal way possible, you get this: Try all subsets of non-adjacent houses. It is a fair place to begin because it matches the surface of the question, yet it does not capture the deeper structure that makes the problem simpler.

Better approach

A calmer way to see House Robber is this: DP: dp[i] = max(dp[i-1], dp[i-2] + nums[i]) — rob this house or skip it. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout House Robber is this: At each house, the best choice is the better of: skip (keep previous max) or rob (add to max from two back). If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

The trap in House Robber usually looks like this: Forgetting that you CAN skip multiple houses — dp[i-1] already accounts for that. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Dynamic Programming Pattern