mediumDynamic ProgrammingDynamic Programming

House Robber

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

Signals to notice

maximize valuecan't pick adjacent elementslinear sequence of choices

Brute force first

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.

The key insight

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.

What must stay true

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.

Easy way to go wrong

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