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)createsnslots (indices0ton - 1), all holding0.set(i, val)changes slotitovalin the live array.snap()freezes the live array and returns the snapshot id:0for the first call, then1,2, ...get(i, snap_id)returns the value slotihad when snapshotsnap_idwas taken.snap_idis always an id thatsnap()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**4calls in total - Target:
setandsnapinO(1),getinO(log c)wherecis the number of changes to sloti; copying the array on everysnapis 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