dynamic-programming · Problem 2 of 5
House Robber
medium
Amazon
Google
You cannot rob two adjacent houses. Return the maximum total you can take from nums.
rob([2, 7, 9, 3, 1]) -> 12Your 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| call | type | expected | result |
|---|---|---|---|
| rob([2,7,9,3,1]) | classic | 12 | — |
| rob([1,2]) | two houses | 2 | — |
| rob([]) | empty | 0 | — |
| rob([5]) | single | 5 | — |
| withheld | hidden | withheld | — |
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