-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1146. Snapshot Array (Array + Binary Search) 20.9.13 Medium
More file actions
180 lines (142 loc) · 4.68 KB
/
1146. Snapshot Array (Array + Binary Search) 20.9.13 Medium
File metadata and controls
180 lines (142 loc) · 4.68 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
Implement a SnapshotArray that supports the following interface:
SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.
void set(index, val) sets the element at the given index to be equal to val.
int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.
int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id
Example 1:
Input: ["SnapshotArray","set","snap","set","get"]
[[3],[0,5],[],[0,6],[0,0]]
Output: [null,null,0,null,5]
Explanation:
SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3
snapshotArr.set(0,5); // Set array[0] = 5
snapshotArr.snap(); // Take a snapshot, return snap_id = 0
snapshotArr.set(0,6);
snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5
Constraints:
1 <= length <= 50000
At most 50000 calls will be made to set, snap, and get.
0 <= index < length
0 <= snap_id < (the total number of times we call snap())
0 <= val <= 10^9
Solution no.1: BF O(n) T and O(n * m) S where n == length, m == number of snapshot => TLE
class SnapshotArray(object):
def __init__(self, length):
"""
:type length: int
"""
self.curr = [0] * length
self.snapshot_list = [[0] for i in range(length)]
self.snap_id = -1
def set(self, index, val):
"""
:type index: int
:type val: int
:rtype: None
"""
self.curr[index] = val
def snap(self):
"""
:rtype: int
"""
self.snap_id += 1
for i in range(len(self.curr)):
self.snapshot_list[i].append(self.curr[i])
return self.snap_id
def get(self, index, snap_id):
"""
:type index: int
:type snap_id: int
:rtype: int
"""
return self.snapshot_list[index][snap_id + 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)
Solution no.2: Binary Search O(logs) T and O(s) S where s is the number of set() call
class SnapshotArray(object):
def __init__(self, length):
"""
:type length: int
"""
self.index_to_snapshot = [[[-1, 0]] for i in range(length)]
self.length = length
self.snap_id = 0
def set(self, index, val):
"""
:type index: int
:type val: int
:rtype: None
"""
self.index_to_snapshot[index].append([self.snap_id, val])
def snap(self):
"""
:rtype: int
"""
self.snap_id += 1
return self.snap_id - 1
def get(self, index, snap_id):
"""
:type index: int
:type snap_id: int
:rtype: int
"""
index_history = self.index_to_snapshot[index]
left, right = 0, len(index_history) - 1
while left <= right:
mid = (left + right) / 2
if index_history[mid][0] <= snap_id:
left = mid + 1
else:
right = mid - 1
return index_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)
Update Version:
class SnapshotArray(object):
def __init__(self, length):
"""
:type length: int
"""
self.index_to_snapshot = [[[0, 0]] for _ in range(length)]
self.snapshot_id = 0
def set(self, index, val):
"""
:type index: int
:type val: int
:rtype: None
"""
self.index_to_snapshot[index].append([val, self.snapshot_id])
def snap(self):
"""
:rtype: int
"""
self.snapshot_id += 1
return self.snapshot_id - 1
def get(self, index, snap_id):
"""
:type index: int
:type snap_id: int
:rtype: int
"""
history = self.index_to_snapshot[index]
left, right = 0, len(history) - 1
while left + 1 < right:
mid = (left + right) / 2
if history[mid][1] > snap_id:
right = mid
else:
left = mid
if history[right][1] > snap_id:
return history[left][0]
return history[right][0]
# 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)