-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathSnapshot Array.java
More file actions
34 lines (28 loc) · 843 Bytes
/
Snapshot Array.java
File metadata and controls
34 lines (28 loc) · 843 Bytes
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
class SnapshotArray {
private final TreeMap<Integer, Integer>[] db;
private int currentSnapId;
public SnapshotArray(int length) {
db = new TreeMap[length];
for (int i = 0; i < length; i++) {
db[i] = new TreeMap<Integer, Integer>();
db[i].put(0, 0);
}
this.currentSnapId = 0;
}
public void set(int index, int val) {
db[index].put(currentSnapId, val);
}
public int snap() {
return currentSnapId++;
}
public int get(int index, int snap_id) {
return db[index].floorEntry(snap_id).getValue();
}
}
/**
* 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);
*/