Editor

Houses in a Circle

#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.

Examples

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.

Constraints

  • 1 <= len(money) <= 500
  • 0 <= money[i] <= 10000

Note

A single house has no neighbours, so the thief can take it.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.