dynamic-programming · Problem 2 of 5

House Robber

medium
Amazon logoAmazon
Google logoGoogle

You cannot rob two adjacent houses. Return the maximum total you can take from nums.

rob([2, 7, 9, 3, 1]) -> 12

Your solution

Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.

Running is free — Submit is what records it. Or press ⌘↩

Tests

5 cases, 1 hidden
calltypeexpectedresult
rob([2,7,9,3,1])classic12
rob([1,2])two houses2
rob([])empty0
rob([5])single5
withheldhiddenwithheld

Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.

Complexity

target time
O(n)
target space
O(1)

A table would be O(n) space and is a fine first version. Only two values are ever read, which is what collapses it to constant.

Hints

Stuck? Hints open one at a time, each giving a little more away.

3 hints left