-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1146.cpp
More file actions
34 lines (30 loc) · 846 Bytes
/
1146.cpp
File metadata and controls
34 lines (30 loc) · 846 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 {
public:
unordered_map<int, map<int, int>> snapshot; // index -> {snapshot id, value}
int snapshowIdx;
SnapshotArray(int length) {
snapshowIdx = 0;
for (int i = 0; i < length; ++i) {
snapshot[i][snapshowIdx] = 0;
}
}
void set(int index, int val) {
snapshot[index][snapshowIdx] = val;
}
int snap() {
snapshowIdx++;
return snapshowIdx - 1;
}
int get(int index, int snap_id) {
auto it = snapshot[index].upper_bound(snap_id);
it = prev(it);
return it->second;
}
};
/**
* 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);
*/