Skip to content

Commit aafae21

Browse files
author
Partho Biswas
committed
1146. Snapshot Array
1 parent 9a9e46c commit aafae21

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ I have solved quite a number of problems from several topics. See the below tabl
151151
|44| [41. First Missing Positive](https://tinyurl.com/tfewwtv)| [Python](https://tinyurl.com/wu6rdaw/41_First_Missing_Positive.py)| --- | Hard | Cyclic Sort, Very important |
152152
|45| **[939. Minimum Area Rectangle](https://tinyurl.com/tcr34w3)** | [Python](https://tinyurl.com/wu6rdaw/939_Minimum_Area_Rectangle.py)| [Art 1](https://tinyurl.com/wfgjaf3) | Medium | Hash and Set, Very important |
153153

154+
154155
</p>
155156
</details>
156157

@@ -296,6 +297,7 @@ I have solved quite a number of problems from several topics. See the below tabl
296297
|20| [658. Find K Closest Elements](https://tinyurl.com/rkpkjfo)| [Python](https://tinyurl.com/wu6rdaw/658_Find_K_Closest_Elements.py)| [Vid 1](https://tinyurl.com/w5ccjoe), [educative.io](https://tinyurl.com/uogulbz) | Medium | 📌 **[Binary Search Template III](https://tinyurl.com/vpsn3w8)**. TODO: Check Heap approach which is not done|
297298
|21| [702. Search in a Sorted Array of Unknown Size](https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/)| [Python](https://tinyurl.com/wu6rdaw/702_Search_in_a_Sorted_Array_of_Unknown_Size.py)| [Educative.io](https://tinyurl.com/qn6uuel) | Medium | 📌 [Binary Search Template I](https://leetcode.com/explore/learn/card/binary-search/125/template-i/938/) |
298299
|22| **[378. Kth Smallest Element in a Sorted Matrix](https://tinyurl.com/shz4289)** | [Python](https://tinyurl.com/wu6rdaw/378_Kth_Smallest_Element_in_a_Sorted_Matrix.py)| [educative.io](https://tinyurl.com/scbjgjd) | Hard | 📌 TODO: Check again the Binary Search approach. Very important |
300+
|23| **[1146. Snapshot Array](https://tinyurl.com/v3nb29v)** | [Python](https://tinyurl.com/wu6rdaw/1146_Snapshot_Array.py)| [Art 1](https://tinyurl.com/v3nb29v) | Medium | Tricky use of binary search |
299301

300302
</p>
301303
</details>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import bisect
2+
class SnapshotArray(object):
3+
4+
def __init__(self, length):
5+
"""
6+
:type length: int
7+
"""
8+
self.array = [[[-1, 0]] for _ in range(length)]
9+
self.currentSnapID = 0
10+
self.length = length
11+
12+
def set(self, index, val):
13+
"""
14+
:type index: int
15+
:type val: int
16+
:rtype: None
17+
"""
18+
self.array[index].append([self.currentSnapID, val])
19+
20+
def snap(self):
21+
"""
22+
:rtype: int
23+
"""
24+
self.currentSnapID += 1
25+
return self.currentSnapID - 1
26+
27+
def get(self, index, snap_id):
28+
"""
29+
:type index: int
30+
:type snap_id: int
31+
:rtype: int
32+
"""
33+
recordIdxforSnapAtIdx = bisect.bisect(self.array[index], [snap_id + 1]) - 1
34+
return self.array[index][recordIdxforSnapAtIdx][1]
35+
36+
37+
38+
# Your SnapshotArray object will be instantiated and called as such:
39+
obj = SnapshotArray(3)
40+
obj.set(0,5)
41+
param_2 = obj.snap()
42+
obj.set(0,6)
43+
v = param_3 = obj.get(0,0)
44+
print(v)

0 commit comments

Comments
 (0)