Snapshot Array
Implement a SnapshotArray that supports the following interface:
SnapshotArray(int length)
initializes an array-like data structure with the given length. Initially, each element equals 0.void set(index, val)
sets the element at the givenindex
to be equal toval
.int snap()
takes a snapshot of the array and returns thesnap_id
: the total number of times we calledsnap()
minus1
.int get(index, snap_id)
returns the value at the givenindex
, at the time we took the snapshot with the givensnap_id
Example 1:
Input: ["SnapshotArray","set","snap","set","get"]
[[3],[0,5],[],[0,6],[0,0]]
Output: [null,null,0,null,5]
Explanation:
SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
snapshotArr.set(0,5); // Set array[0] = 5
snapshotArr.snap(); // Take a snapshot, return snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5
Constraints:
1 <= length <= 5 * 104
0 <= index < length
0 <= val <= 109
0 <= snap_id <
(the total number of times we callsnap()
)- At most
5 * 104
calls will be made toset
,snap
, andget
.
Solution
We wish to find the pos
for the most recent value at the time we took the snapshot with the given snap_id
,
we are trying to find the rightmost index in history=histories[i]
such that the snap_id
at history[pos]
is less or equal to the target snap_id
(a[i][0] <= snap_id
).
This means that the feasible function is a[i][0] <= snap_id
, whenever this is true, we must check the positions on its right to find the rightmost position that makes this condition hold.
Implementation
class SnapshotArray(object):
def __init__(self, n):
# set up histories so that each index has its own history
self.histories = [[[-1, 0]] for _ in range(n)]
self.snap_id = 0
def set(self, index, val):
self.histories[index].append([self.snap_id, val])
def snap(self):
self.snap_id += 1
return self.snap_id - 1
def get(self, index, snap_id):
left, right, pos = 0, len(self.histories[index])-1, -1
while left <= right:
mid = (left+right) // 2
if self.histories[index][mid][0] <= snap_id:
left = mid + 1
pos = mid
else:
right = mid - 1
return self.histories[index][pos][1]
Ready to land your dream job?
Unlock your dream job with a 2-minute evaluator for a personalized learning plan!
Start EvaluatorWhat's the relationship between a tree and a graph?
Recommended Readings
LeetCode Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way we
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!