Problem 560898 · medium · Phase 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 = [2, 7, 9, 3, 1]
Output: 11
Explanation: open vaults 0 and 2 (2 + 9); vault 4 is adjacent to vault 0, so it stays shut.

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…