Problem 425861 · medium · Level 04 Non-Linear Data Structures

Seat Map With Snapshots

design · binary search · versioning · classes

A ticketing system keeps an array of n seat prices and lets staff freeze the current prices as numbered snapshots. Design a class SeatMap:

  • SeatMap(n) creates n slots (indices 0 to n - 1), all holding 0.
  • set(i, val) changes slot i to val in the live array.
  • snap() freezes the live array and returns the snapshot id: 0 for the first call, then 1, 2, ...
  • get(i, snap_id) returns the value slot i had when snapshot snap_id was taken. snap_id is always an id that snap() has already returned.

Examples

ops:  ["SeatMap", "set", "set", "snap", "set", "snap", "set", "get", "get", "get", "get"]
args: [[4], [2, 15], [3, 6], [], [2, 11], [], [3, 8], [2, 0], [2, 1], [3, 1], [1, 0]]
Output: [None, None, None, 0, None, 1, None, 15, 11, 6, 0]
Explanation: snapshot 0 saw slot 2 = 15; the change to 11 only shows up in snapshot 1. Slot 3 was
changed to 8 after snapshot 1, so snapshot 1 still sees 6. Slot 1 was never changed.

Constraints

  • 1 <= n <= 10**4, 0 <= i < n, values are integers
  • Up to 10**4 calls in total
  • Target: set and snap in O(1), get in O(log c) where c is the number of changes to slot i; copying the array on every snap is too slow and too big

Goals

  • Store only the changes to each slot instead of copying the whole array per snapshot
  • Find the value of a slot at an old snapshot with binary search
Starting Python…