Show problems as
Classic problem #213MediumArrayDynamic Programming

House Robber II

Real-world version: Restocking a Ring of Kiosks
Tracked in this browser only
Read House Robber II on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Vending kiosks stand in a circle around a plaza. Kiosk i will take crates[i] crates of stock tonight, and the array closes on itself: the last kiosk and the first kiosk stand side by side.

One van does the round, and it cannot serve two kiosks that stand next to each other, because the plaza only opens one bay at a time and neighbouring bays share a shutter. Serving no kiosk at all is allowed.

Return the largest number of crates the van can deliver in a single night.

Examples

Example 1

Input
crates = [7, 4, 9, 5]
Output
16

Serving the first and third kiosk delivers 7 plus 9 crates; the other legal pair, the second and fourth, only manages 9 in total.

Example 2

Input
crates = [6, 6, 6]
Output
6

On a ring of three every kiosk touches both others, so the van can serve only one of them.

Example 3

Input
crates = [0, 0]
Output
0

Neither kiosk needs stock, so the van delivers nothing.

Constraints

  • 1 <= crates.length <= 100
  • 0 <= crates[i] <= 1000

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def max_ring_restock(crates: list[int]) -> int:
Java
public int maxRingRestock(int[] crates)