Dynamic Programming21 sections · 916 units
Open in Course

Lessons from House Robber

summary

House Robber teaches a core DP pattern: When can't take adjacent elements, your transition considers two choices: skip the current element, or take it plus the best answer from two positions back.

The recurrence dp[i]=max(dp[i1],dp[i2]+nums[i])dp[i] = \max(dp[i-1], dp[i-2] + nums[i]) captures this exactly. Skip house ii and keep dp[i1]dp[i-1], or rob house ii and add to dp[i2]dp[i-2].

This pattern appears in many problems: picking non-adjacent elements, scheduling with cooldowns, or any situation where taking one option blocks the next. the space reduction: you only need the last two values. This reduces O(n)O(n) space to O(1)O(1). Watch for this pattern in other DP problems.