Skip to content

Commit 426dc38

Browse files
committed
Im a dumbass that even after understanding the concept and almost solving the question i still had to go and copy the code a bit, jesus christ bitch use a fucking notebook to write the questions and do them in rough first then do the implementation, to be the best i need practice dumbarse
1 parent 0783580 commit 426dc38

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

1146. Snapshot Array.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class SnapshotArray(object):
2+
3+
def __init__(self, length):
4+
self.arr = [[(0,0)] for i in range(length)]
5+
self.snap_id = 0;
6+
7+
8+
def set(self, index, val):
9+
self.arr[index].append((self.snap_id,val))
10+
11+
12+
def snap(self):
13+
self.snap_id+=1
14+
return self.snap_id-1
15+
16+
17+
def get(self, index, snap_id):
18+
history = self.arr[index]
19+
left = 0
20+
right = len(history) - 1
21+
22+
while(left<=right):
23+
mid = (left+right)//2
24+
25+
if(history[mid][0]<=snap_id):
26+
left = mid+1
27+
else:
28+
right = mid-1
29+
return history[right][1];
30+
31+
32+
33+
# Your SnapshotArray object will be instantiated and called as such:
34+
# obj = SnapshotArray(length)
35+
# obj.set(index,val)
36+
# param_2 = obj.snap()
37+
# param_3 = obj.get(index,snap_id)

0 commit comments

Comments
 (0)