#303 · Dynamic programming · Intermediate · 70 XP · about 12 min
The houses of a small village stand in a ring around the green, and money[i] is the cash hidden in house i. Neighbouring houses share an alarm, so a thief can never take from two houses that stand side by side. Because the houses form a ring, the last house in the list is also a neighbour of the first.
Write rob_ring(money) that returns the largest total the thief can take.
rob_ring([6, 1, 1, 6]) # returns 7 rob_ring([4, 2, 3, 9, 1]) # returns 13 rob_ring([5]) # returns 5
In the first example the two houses holding 6 are neighbours around the ring, so the thief can take only one of them.
1 <= len(money) <= 5000 <= money[i] <= 10000A single house has no neighbours, so the thief can take it.
First solves only, ranked by stars and then time.