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", "snap", "set", "set", "get", "snap", "get", "get", "get"]
args: [[3], [0, 7], [], [0, 9], [2, 4], [0, 0], [], [0, 1], [2, 0], [1, 1]]
Output: [None, None, 0, None, None, 7, 1, 9, 0, 0]
Explanation: snapshot 0 saw slot 0 = 7; the later changes only show up in snapshot 1.
Slot 2 was still 0 in snapshot 0, and 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