From 2edbe48aa659fb0aab488290085fdae6d041933d Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Thu, 2 Oct 2025 12:50:44 -0400 Subject: [PATCH] Time: 188 ms (58.01%), Space: 44.2 MB (76.9%) - LeetHub --- 1146-snapshot-array/1146-snapshot-array.py | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 1146-snapshot-array/1146-snapshot-array.py diff --git a/1146-snapshot-array/1146-snapshot-array.py b/1146-snapshot-array/1146-snapshot-array.py new file mode 100644 index 0000000..23c17f1 --- /dev/null +++ b/1146-snapshot-array/1146-snapshot-array.py @@ -0,0 +1,41 @@ +class SnapshotArray: + + def __init__(self, length: int): + self.data = [0] * length + self.snapshotChanges = [[] for i in range(length)] + self.snapId = 0 + + + def set(self, index: int, val: int) -> None: + self.data[index] = val + changes = self.snapshotChanges[index] + if len(changes) > 0 and changes[-1][0] == self.snapId: + changes[-1][1] = val + else: + self.snapshotChanges[index].append([self.snapId, val]) + + + def snap(self) -> int: + self.snapId += 1 + return self.snapId - 1 + + + def get(self, index: int, snap_id: int) -> int: + changes = self.snapshotChanges[index] + + insert_index = bisect.bisect(changes, snap_id, key=lambda c : c[0]) + + if insert_index == 0: + return 0 + + return changes[insert_index - 1][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) \ No newline at end of file