WorkspaceProblem lab
ALGORITHM VISUALIZER

House Robber

Opening the interactive workspace…

Understanding House Robber

Return the largest sum you can collect from a line of houses without choosing two adjacent houses. Each house contains a nonnegative amount.

How it works

For each prefix, compare skipping the newest house with taking it after the best compatible earlier prefix.

Let dp[i] be the best sum from the first i houses. Use max(dp[i − 1], dp[i − 2] + nums[i − 1]).

Try an example

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

Output: 12

Choose houses 0, 2, and 4 for 2 + 9 + 1 = 12.

Constraints

  • At most 64 houses with amounts between 0 and 10,000.
  • You may choose no houses; an empty street returns 0.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity