PythonStudio
Interview Problems

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 compiler