Snapshot Array
Implement a SnapshotArray class: an array of integers that can save snapshots of itself and read values from any earlier snapshot.
SnapshotArray(length)creates an array oflengthelements, all starting at0.set(index, val)sets the element atindextoval.snap()takes a snapshot and returns its id. The first snapshot has id0, and each call returns the previous id plus one.get(index, snap_id)returns the value atindexat the moment snapshotsnap_idwas taken.
The tests run a sequence of operations on one array and check the values returned by every snap and get call, in order.
Example 1
Input: ["SnapshotArray", "set", "snap", "set", "get"] [[3], [0, 5], [], [0, 6], [0, 0]] Output: [0, 5]
The array starts as [0, 0, 0]. Setting index 0 to 5 and taking a snapshot returns id 0. Setting index 0 to 6 afterward doesn't change snapshot 0, so get(0, 0) returns 5.
Example 2
Input: ["SnapshotArray", "snap", "get", "set", "snap", "get"] [[2], [], [1, 0], [1, 4], [], [1, 1]] Output: [0, 0, 1, 4]
Index 1 is still 0 in snapshot 0. It's set to 4 before snapshot 1 is taken, so snapshot 1 sees 4.
Constraints
1 <= length <= 500000 <= index < lengthand0 <= val <= 10^9getis only called with asnap_idthat has already been taken.- At most
50000calls are made in total.
Approach
Copying the full array on every snap costs O(length) time and memory per snapshot, which is far too much when there are tens of thousands of both. Most elements don't change between snapshots, so store only the changes.
For each index, keep a list of (snap_id, value) pairs, starting with (-1, 0) so every index has a value before it's ever set. set tags the change with the current snapshot id, the one the next snap call will return. If the last change already has that id, overwrite it, since only the final value before a snapshot matters. snap just increments a counter. Because ids only increase, each list stays sorted, so get(index, snap_id) can binary search for the last change whose id is at or below snap_id.
Steps
- Give each index a change list
[(-1, 0)]and set the current snapshot id to0. - In
set, overwrite the last change if its id equals the current id. Otherwise, append(current id, val). - In
snap, return the current id and increase it by 1. - In
get, binary search the index's list for the last change with id<= snap_idand return its value.
from bisect import bisect_right
class SnapshotArray:
def __init__(self, length: int):
# for each index, a list of (snap_id, value) changes in snap_id order
self.history = [[(-1, 0)] for _ in range(length)]
self.snap_id = 0
def set(self, index: int, val: int) -> None:
changes = self.history[index]
if changes[-1][0] == self.snap_id:
changes[-1] = (self.snap_id, val) # overwrite a change made since the last snap
else:
changes.append((self.snap_id, val))
def snap(self) -> int:
self.snap_id += 1
return self.snap_id - 1
def get(self, index: int, snap_id: int) -> int:
changes = self.history[index]
# the last change made at or before snap_id
i = bisect_right(changes, (snap_id, float("inf"))) - 1
return changes[i][1]The Java version uses a TreeMap per index, where floorEntry does the same search as the binary search in the other languages.
Complexity
- Time:
O(1)forsetandsnap, andO(log c)forget, wherecis the number of changes stored for that index. - Space:
O(length + s), wheresis the number ofsetcalls.
Common mistakes
- Copying the array on every
snap, which runs out of time and memory. - Tagging a
setwith the id of the last snapshot taken instead of the next one. Changes made after snapshot 0 belong to snapshot 1. - Searching for the first change at or after
snap_id. You need the last one at or before it.