Problem 560898 · medium · Level 05 Advanced Algorithms & Graphs

Ring of Vaults

dynamic programming · 1-D dp · circular arrays · case split

Vaults stand in a circle; vault i holds gold[i] bars and the last vault is next to the first. Opening a vault triggers the alarms of both neighbours, so no two adjacent vaults may be opened. Return the maximum number of bars you can take.

Examples

Input:  gold = [6, 3, 8, 4, 9]
Output: 17
Explanation: open vaults 2 and 4 (8 + 9). Vaults 0, 2 and 4 would give 23, but vault 4 is next to vault 0.

Input:  gold = [3, 8]
Output: 8

Constraints

  • 1 <= len(gold) <= 10**5
  • 0 <= gold[i] <= 10**4
  • Target complexity: O(n) time.

Goals

  • Reduce a circular constraint to two runs of a linear recurrence
  • Reuse a helper for the linear take-or-skip problem
Starting Python…