-
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
package com.fishercoder.solutions.secondthousand;
import java.util.*;
public class _1146 {
public static class Solution1 {
public static class SnapshotArray {
TreeMap<Integer, Integer>[]
snapshots; // using this data structure is much more efficient in terms of
// storage, esp. if snap() calls happen frequently
int snapId;
public SnapshotArray(int length) {
snapshots = new TreeMap[length];
snapId = 0;
for (int i = 0; i < length; i++) {
snapshots[i] = new TreeMap<>();
snapshots[i].put(0, 0);
}
}
public void set(int index, int val) {
snapshots[index].put(snapId, val);
}
public int snap() {
return snapId++;
}
public int get(int index, int snapId) {
return snapshots[index].floorEntry(snapId).getValue();
}
}
}
}