iq.lab
Python starts when a code cell comes near or you run one
mediumDynamic programming, 1D target 25 min

House robber

A thief plans one night on a street of houses in a row. money[i] is the cash in house i. Robbing two houses that stand next to each other sets off an alarm, so the thief never robs two neighbors. Skipping several houses in a row is fine.

Return the most cash the thief can collect.

Example 1
Inputmoney = [3, 10, 3, 1, 2]Output12

Rob houses 1 and 4: 10 + 2 = 12. Robbing every other house gives only 3 + 3 + 2 = 8 or 10 + 1 = 11.

Example 2
Inputmoney = [5, 1, 1, 5]Output10

Rob the first and the last house, skipping the two in the middle.

Example 3
Inputmoney = [6, 7, 6]Output12

Robbing the richest house first takes the 7 and blocks both 6s. The two 6s together are worth more.

Constraints
  • 1 ≤ len(money) ≤ 100

  • 0 ≤ money[i] ≤ 500

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.