- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1146.java
35 lines (29 loc) · 1.03 KB
/
_1146.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.*;
publicclass_1146 {
publicstaticclassSolution1 {
publicstaticclassSnapshotArray {
TreeMap<Integer, Integer>[]
snapshots; // using this data structure is much more efficient in terms of
// storage, esp. if snap() calls happen frequently
intsnapId;
publicSnapshotArray(intlength) {
snapshots = newTreeMap[length];
snapId = 0;
for (inti = 0; i < length; i++) {
snapshots[i] = newTreeMap<>();
snapshots[i].put(0, 0);
}
}
publicvoidset(intindex, intval) {
snapshots[index].put(snapId, val);
}
publicintsnap() {
returnsnapId++;
}
publicintget(intindex, intsnapId) {
returnsnapshots[index].floorEntry(snapId).getValue();
}
}
}
}