-
Notifications
You must be signed in to change notification settings - Fork 43
/
SnapshotArray.java
301 lines (264 loc) · 10 KB
/
SnapshotArray.java
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package LeetCodeJava.Design;
// https://leetcode.com/problems/snapshot-array/description/
import java.util.*;
/**
* 1146. Snapshot Array
* Medium
* Topics
* Companies
* Hint
* 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 <= 5 * 104
* 0 <= index < length
* 0 <= val <= 109
* 0 <= snap_id < (the total number of times we call snap())
* At most 5 * 104 calls will be made to set, snap, and get.
*
*/
public class SnapshotArray {
/**
* Your SnapshotArray object will be instantiated and called as such:
* SnapshotArray obj = new SnapshotArray(length);
* obj.set(index,val);
* int param_2 = obj.snap();
* int param_3 = obj.get(index,snap_id);
*/
// V0
// (TLE) : TODO : optimize
// class SnapshotArray_ {
//
// Integer[] elements;
// Map<Integer, Integer[]> snapshotMap;
// Integer snapshotCount;
//
// public SnapshotArray_(int length) {
// this.elements = new Integer[length];
// this.snapshotMap = new HashMap<>();
// this.snapshotCount = 0;
// // Store the initial snapshot (snapshot 0)
// this.snapshotMap.put(this.snapshotCount, this.elements.clone());
// }
//
// public void set(int index, int val) {
// // Set value in the current snapshot (current version of elements)
// this.elements[index] = val;
// }
//
// public int snap() {
// // Take a snapshot of the current elements array by creating a new copy
// snapshotMap.put(snapshotCount, elements.clone());
// // Increment snapshotCount to prepare for the next snapshot
// return snapshotCount++;
// }
//
// public int get(int index, int snap_id) {
// // Retrieve the value from the snapshot with the given snap_id
// return snapshotMap.get(snap_id)[index];
// }
// }
// V0_1
// IDEA : (fix by GPT)
/**
* Key Optimizations
*
* 1. Sparse Storage:
* •Instead of copying the whole array for each snapshot,
* the TreeMap for each index only stores the values that
* have been set at different snapshot versions.
* This avoids unnecessary duplication of data.
*
* 2. Efficient Retrieval:
* •Using TreeMap.floorEntry() allows us to efficiently
* retrieve the value of an index at the given snapshot,
* or the most recent value before the snapshot.
*
* 3. Memory Efficiency:
* •The memory usage is optimized because we store
* only the changes at each snapshot. If no change
* occurs for an element, we do not store multiple
* copies of the same value.
*
* -> This approach ensures that both set() and get() operations
* remain efficient, with logarithmic time complexity due to
* the use of TreeMap, while significantly reducing memory
* usage compared to the original approach.
*
*/
class SnapshotArray_0_1 {
private int snapId;
private Map<Integer, TreeMap<Integer, Integer>> snapshots;
public SnapshotArray_0_1(int length) {
this.snapId = 0;
this.snapshots = new HashMap<>();
for (int i = 0; i < length; i++) {
snapshots.put(i, new TreeMap<>());
snapshots.get(i).put(0, 0); // Initially, every element is 0 at snap_id 0
}
}
public void set(int index, int val) {
snapshots.get(index).put(snapId, val);
}
public int snap() {
return snapId++;
}
public int get(int index, int snap_id) {
// Get the greatest key less than or equal to snap_id
return snapshots.get(index).floorEntry(snap_id).getValue();
}
}
// V0_2
// IDEA : HASHMAP + LIST (fixed by gpt) + binary search
class SnapshotArray_0_2 {
private int snapId;
private Map<Integer, List<int[]>> snapshots;
public SnapshotArray_0_2(int length) {
this.snapId = 0;
this.snapshots = new HashMap<>();
// Initialize each index with a snapshot at snapId 0, where the value is 0
for (int i = 0; i < length; i++) {
snapshots.put(i, new ArrayList<>());
snapshots.get(i).add(new int[]{0, 0}); // {snapId, value}
}
}
public void set(int index, int val) {
// Store the new value along with the current snapId
List<int[]> snapshotList = snapshots.get(index);
// If the last snapshot has the same snapId, just update the value
if (!snapshotList.isEmpty() && snapshotList.get(snapshotList.size() - 1)[0] == snapId) {
snapshotList.get(snapshotList.size() - 1)[1] = val;
} else {
snapshotList.add(new int[]{snapId, val});
}
}
public int snap() {
return snapId++;
}
public int get(int index, int snap_id) {
List<int[]> snapshotList = snapshots.get(index);
// Perform a binary search to find the largest snapId <= snap_id
int low = 0, high = snapshotList.size() - 1;
while (low < high) {
int mid = (low + high + 1) / 2;
if (snapshotList.get(mid)[0] <= snap_id) {
low = mid; // Move right if the current snapId is valid
} else {
high = mid - 1; // Move left if the current snapId is too large
}
}
return snapshotList.get(low)[1];
}
}
// V1
// IDEA : Binary Search
// https://leetcode.com/problems/snapshot-array/editorial/
class SnapshotArray_1_1 {
int snapId = 0;
TreeMap<Integer, Integer>[] historyRecords;
public SnapshotArray_1_1(int length) {
historyRecords = new TreeMap[length];
for (int i = 0; i < length; i++) {
historyRecords[i] = new TreeMap<Integer, Integer>();
historyRecords[i].put(0, 0);
}
}
public void set(int index, int val) {
historyRecords[index].put(snapId, val);
}
public int snap() {
return snapId++;
}
public int get(int index, int snapId) {
return historyRecords[index].floorEntry(snapId).getValue();
}
}
// V2
// IDEA : HASHMAP
// https://leetcode.com/problems/snapshot-array/solutions/350574/java-python-3-3-codes-w-analysis-store-difference-by-hashmap-and-treemap-respectively/
class SnapshotArray_2_1 {
private List<Map<Integer, Integer>> shot;
private Map<Integer, Integer> diff;
public SnapshotArray_2_1(int length) {
shot = new ArrayList<>(length);
diff = new HashMap<>(length);
}
public void set(int index, int val) {
diff.put(index, val);
}
public int snap() {
shot.add(diff);
diff = new HashMap<>();
return shot.size() - 1;
}
public int get(int index, int snap_id) {
for (int i = snap_id; i >= 0; --i)
if (shot.get(i).containsKey(index))
return shot.get(i).get(index);
return 0;
}
}
// V2-2
// https://leetcode.com/problems/snapshot-array/solutions/350574/java-python-3-3-codes-w-analysis-store-difference-by-hashmap-and-treemap-respectively/
// IDEA : BINARY SEARCH
class SnapshotArray_2_2 {
private int snapId;
private List<List<int[]>> shot;
public SnapshotArray_2_2(int length) {
this.snapId = 0;
this.shot = new ArrayList<>();
// Initialize the list with [-1, 0] for each index
for (int i = 0; i < length; i++) {
List<int[]> snapshotList = new ArrayList<>();
snapshotList.add(new int[]{-1, 0}); // Add [-1, 0] as the initial state
this.shot.add(snapshotList);
}
}
public void set(int index, int val) {
List<int[]> a = this.shot.get(index);
if (a.get(a.size() - 1)[0] == this.snapId) {
a.get(a.size() - 1)[1] = val; // Update if snapId matches the last entry
} else {
a.add(new int[]{this.snapId, val}); // Otherwise add new entry
}
}
public int snap() {
this.snapId++;
return this.snapId - 1; // Return the current snapId, then increment
}
public int get(int index, int snapId) {
List<int[]> a = this.shot.get(index);
int low = 0, high = a.size() - 1;
// Binary search to find the correct snapshot using snapId
while (low < high) {
int mid = (low + high + 1) / 2;
if (a.get(mid)[0] <= snapId) {
low = mid;
} else {
high = mid - 1;
}
}
return a.get(low)[1]; // Return the value for the found snapshot
}
}
}