All problems
0188MediumArrayDynamic Programming

Restocking a Ring of Kiosks

Tracked in this browser only
Write code

Trains the technique from

LeetCode 213House Robber II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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