forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapshot Array.java
41 lines (34 loc) · 988 Bytes
/
Snapshot Array.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
// Runtime: 43 ms (Top 90.65%) | Memory: 81.4 MB (Top 44.17%)
class SnapshotArray {
TreeMap<Integer,Integer>[] snapshotArray;
int currSnapId;
public SnapshotArray(int length) {
snapshotArray = new TreeMap[length];
for(int i=0;i<length;i++)
{
snapshotArray[i] = new TreeMap();
}
currSnapId =0;
}
public void set(int index, int val) {
snapshotArray[index].put(currSnapId,val);
}
public int snap() {
return currSnapId++;
}
public int get(int index, int snap_id) {
Integer lowerKey = snapshotArray[index].floorKey(snap_id);
if(lowerKey !=null)
{
return snapshotArray[index].get(lowerKey);
}
return 0;
}
}
/**
* 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);
*/