Snapshot Array
Implement a SnapshotArray of a given length, initialized to all zeros. set(index, val) writes a value. snap() takes a snapshot and returns the id (0-based, one less than the number of snaps taken). get(index, snap_id) returns the value at that index at the time the given snapshot was taken.
Open official problem prompt ↗Answer 'what was this cell's value at snapshot t?' efficiently while spending memory only on cells that actually changed.
Like a document's version history: instead of photocopying the whole document on every save, you record only the edited lines with a version stamp, then jump to the right version when asked.
- Input
- SnapshotArray(3); set(0, 5); snap(); set(0, 6); get(0, 0)
- Output
- [null, null, 0, null, 5]
- Why
- set(0,5) then snap() returns id 0 while index 0 held 5; the later set(0,6) happens after snapshot 0, so get(0,0) still reports 5.
1 <= length <= 5 * 10^40 <= index < length0 <= val <= 10^90 <= snap_id < (number of times snap has been called)At most 5 * 10^4 calls total to set, snap, and get