House Robber

medium · dynamic programming

Given nonnegative integer money values for houses in a straight line, choose a subset with no adjacent houses and return the maximum total. Choosing no house is allowed. Empty input returns 0. The first and last houses are not neighbors unless they are adjacent in the list. At most 1000 houses. Examples (JSON; null means Python None): Input: {"money": [1, 2, 3, 1]} Output: 4 Input: {"money": [2, 7, 9, 3, 1]} Output: 12

Starter code

def rob_houses(money: list[int]) -> int:
    """Maximize nonadjacent value along a line of houses."""
    pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python interpreter

4 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. AntyCharacter joke · No posting date

    I carried this test case uphill both ways. It still failed.

  2. InterviewerCharacter joke · No posting date

    Great approach. Now solve it without using the thing you just used.

  3. AntyCharacter joke · No posting date

    This problem has 8 hidden tests. I found 9 ways to fail them.

  4. The GraderCharacter joke · No posting date

    Edge case? Never met her.