TRT
Sign in
mediumAdvancedLeetCode#take or skip

House robber

Maximize the loot from houses on a street without robbing two neighbors, with a take-or-skip recurrence.

The Runtime Theory Team1 min read
Taught inOne-dimensional problemsPro — members onlyMany problems over a sequence have a DP with one cell per position: the best answer for the prefix ending there. Take-or-skip, counting the ways, and best-ending-here cover most of them.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

innums = [1, 2, 3, 1]

out4

innums = [2, 7, 9, 3, 1]

out12

innums = [0]

out0

Each house holds some money; robbing two adjacent houses triggers an alarm. Return the most you can rob.

Before you code

For each house, compare the best without it and the best with it, which rules out its left neighbor.

Hints

  1. dp[i] = max(dp[i-1], dp[i-2] + nums[i]).
  2. Handle one and two houses as base cases.
  3. Reduce to two variables: O(1) space.

Production & complexity notes

When solving House robber on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in One-dimensional problems to reinforce the underlying pattern.

What it practices

The take-or-skip recurrence, from One-dimensional problems.

Not started

Sign in to save your learning progress.

Sign in to save