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:
1SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
2
3snapshotArr.set(0,5); // Set array[0] = 5
4
5snapshotArr.snap(); // Take a snapshot, return snap_id = 0
6
7snapshotArr.set(0,6);
8
9snapshotArr.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
1class SnapshotArray(object):
2
3def __init__(self, n):
4 # set up histories so that each index has its own history
5 self.histories = [[[-1, 0]] for _ in range(n)]
6 self.snap_id = 0
7
8def set(self, index, val):
9 self.histories[index].append([self.snap_id, val])
10
11def snap(self):
12 self.snap_id += 1
13 return self.snap_id - 1
14
15def get(self, index, snap_id):
16 left, right, pos = 0, len(self.histories[index])-1, -1
17 while left <= right:
18 mid = (left+right) // 2
19 if self.histories[index][mid][0] <= snap_id:
20 left = mid + 1
21 pos = mid
22 else:
23 right = mid - 1
24 return self.histories[index][pos][1]
Which of the following uses divide and conquer strategy?
Which of the following shows the order of node visit in a Breadth-first Search?
Solution Implementation
Which of these pictures shows the visit order of a depth-first search?
Which one best describes the time complexity of the following code?
1int factorial(int n) {
2 if (n < 0) {
3 return -1;
4 } else if (n == 0) {
5 return 1;
6 } else {
7 return n * factorial(n - 1);
8 }
9}
Recommended Readings
Top Patterns to Conquer the Technical Coding Interview Should the written word bore you fear not A delightful video alternative awaits iframe width 560 height 315 src https www youtube com embed LW8Io6IPYHw title YouTube video player frameborder 0 allow accelerometer autoplay clipboard write encrypted media gyroscope picture in picture
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
Got a question? Ask the Teaching Assistant anything you don't understand.
Still not clear? Ask in the Forum, Discord or Submit the part you don't understand to our editors.