-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1146_Snapshot-Array.cpp
More file actions
39 lines (35 loc) · 950 Bytes
/
1146_Snapshot-Array.cpp
File metadata and controls
39 lines (35 loc) · 950 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
35
36
37
38
39
class SnapshotArray {
public:
int st_length;
int time;
// index -> snap_id, val
// use map for binary search of snap_id.
vector<map<int, int>> st;
SnapshotArray(int length) {
st_length = length;
time = 0;
st.resize(length);
for (int i = 0; i < length; ++i) {
st[i][time] = 0;
}
}
void set(int index, int val) { st[index][time] = val; }
int snap() {
time++;
return time - 1;
}
int get(int index, int snap_id) {
if (st[index].find(snap_id) == st[index].end()) {
auto it = --st[index].lower_bound(snap_id);
return it->second;
}
return st[index][snap_id];
}
};
/**
* 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);
*/