Skip to content

Commit aee0d8c

Browse files
authored
Update snapshot-array.py
1 parent 1f37eba commit aee0d8c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Python/snapshot-array.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self, length):
1212
"""
1313
:type length: int
1414
"""
15-
self.__A = collections.defaultdict(lambda: [(-1, 0)])
15+
self.__A = collections.defaultdict(lambda: [[0, 0]])
1616
self.__snap_id = 0
1717

1818

@@ -22,7 +22,10 @@ def set(self, index, val):
2222
:type val: int
2323
:rtype: None
2424
"""
25-
self.__A[index].append((self.__snap_id, val))
25+
if self.__A[index][-1][0] == self.__snap_id:
26+
self.__A[index][-1][1] = val
27+
else:
28+
self.__A[index].append([self.__snap_id, val])
2629

2730

2831
def snap(self):
@@ -39,5 +42,5 @@ def get(self, index, snap_id):
3942
:type snap_id: int
4043
:rtype: int
4144
"""
42-
i = bisect.bisect_right(self.__A[index], (snap_id+1, 0)) - 1
45+
i = bisect.bisect_left(self.__A[index], [snap_id+1, float("-inf")]) - 1
4346
return self.__A[index][i][1]

0 commit comments

Comments
 (0)