File tree 1 file changed +7
-8
lines changed
scripts/algorithms/S/Snapshot Array
1 file changed +7
-8
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 43 ms (Top 90.65%) | Memory: 81.4 MB (Top 44.17%)
1
2
class SnapshotArray {
2
3
3
4
TreeMap <Integer ,Integer >[] snapshotArray ;
4
5
int currSnapId ;
5
-
6
+
6
7
public SnapshotArray (int length ) {
7
8
snapshotArray = new TreeMap [length ];
8
9
for (int i =0 ;i <length ;i ++)
@@ -11,18 +12,18 @@ public SnapshotArray(int length) {
11
12
}
12
13
currSnapId =0 ;
13
14
}
14
-
15
+
15
16
public void set (int index , int val ) {
16
17
snapshotArray [index ].put (currSnapId ,val );
17
18
}
18
-
19
+
19
20
public int snap () {
20
21
return currSnapId ++;
21
22
}
22
-
23
+
23
24
public int get (int index , int snap_id ) {
24
25
Integer lowerKey = snapshotArray [index ].floorKey (snap_id );
25
-
26
+
26
27
if (lowerKey !=null )
27
28
{
28
29
return snapshotArray [index ].get (lowerKey );
@@ -31,12 +32,10 @@ public int get(int index, int snap_id) {
31
32
}
32
33
}
33
34
34
-
35
-
36
35
/**
37
36
* Your SnapshotArray object will be instantiated and called as such:
38
37
* SnapshotArray obj = new SnapshotArray(length);
39
38
* obj.set(index,val);
40
39
* int param_2 = obj.snap();
41
40
* int param_3 = obj.get(index,snap_id);
42
- */
41
+ */
You can’t perform that action at this time.
0 commit comments