00:00

Problem Statement

Input/Output Examples

Visual Explanation

Only the best totals for the previous two houses are needed at each step, so two variables replace the whole table.

Approaches

Both approaches use the same choice: rob or skip. One works bottom-up with two variables; the other recurses with a cache.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of houses. Each house is considered once; the recursive version also keeps a cache and a call stack.

Interview Tips and Recommended Approach

  • State the choice clearly: skip this house, or take it and skip the previous one.
  • Do not use a greedy rule. Taking every other house fails on [1, 3, 1, 3, 100].
  • Know the circular version. If the first and last houses are neighbours, solve twice: without the first, and without the last.
  • Know the tree version, where each node returns two values: robbed and not robbed.

Recommended Approach: Two Rolling Variables

Why: it solves the problem in one pass with two variables and is a clear first example of dynamic programming.