-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1146. Snapshot Array.py
More file actions
37 lines (25 loc) · 868 Bytes
/
1146. Snapshot Array.py
File metadata and controls
37 lines (25 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class SnapshotArray(object):
def __init__(self, length):
self.arr = [[(0,0)] for i in range(length)]
self.snap_id = 0;
def set(self, index, val):
self.arr[index].append((self.snap_id,val))
def snap(self):
self.snap_id+=1
return self.snap_id-1
def get(self, index, snap_id):
history = self.arr[index]
left = 0
right = len(history) - 1
while(left<=right):
mid = (left+right)//2
if(history[mid][0]<=snap_id):
left = mid+1
else:
right = mid-1
return history[right][1];
# Your SnapshotArray object will be instantiated and called as such:
# obj = SnapshotArray(length)
# obj.set(index,val)
# param_2 = obj.snap()
# param_3 = obj.get(index,snap_id)