#198

House Robber

MediumDPHas vizArrayDynamic Programming
Related studio

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 are 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.

Example 1

Input: nums = [1,2,3,1]

Output: 4

Rob house 1 (1) and then house 3 (3).

Example 2

Input: nums = [2,7,9,3,1]

Output: 12

Rob houses 1, 3, and 5: 2+9+1 = 12.

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 400
Loading visualization…

Explanation

House Robber is a Medium interview problem tagged [Array, Dynamic Programming]. The winning approach is typically **DP**.

Intuition

Overlapping subproblems are solved once and reused.

Approach

Define dp state → write transition → fill base cases → iterate in dependency order.

  1. Sentence-define dp[i] (or dp[i][j]).
  2. Relate to smaller states.
  3. Initialize bases.
  4. Loop correctly.
  5. Return the goal state.

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateDP

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n)

Space: O(n)

Before coding, restate the problem, name the pattern (DP), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #198 House Robber as understood when the pattern clicks.