MediumDynamic Programming

House Robber II

The houses stand in a circle, so the first and last houses are neighbours. You can't rob two neighbouring houses. Return the most money you can rob.

nums = [3,4,3] → 4nums = [2,9,8,3,6] → 15

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

The circle

House 0 and the last house are neighbours (red line)

Time and space complexity

n = len(nums). The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. 1 or more amounts from 0 to 200; drawings use at most 6 houses for plain recursion, 10 for top-down and 12 for the loops.