-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshotArray.cpp
More file actions
36 lines (29 loc) · 780 Bytes
/
snapshotArray.cpp
File metadata and controls
36 lines (29 loc) · 780 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
// Source: https://leetcode.com/problems/snapshot-array/
// Author: Miao Zhang
// Date: 2021-04-13
class SnapshotArray {
public:
SnapshotArray(int length): id_(0), vals_(length) {
}
void set(int index, int val) {
vals_[index][id_] = val;
}
int snap() {
return id_++;
}
int get(int index, int snap_id) {
auto it = vals_[index].upper_bound(snap_id);
if (it == begin(vals_[index])) return 0;
return prev(it)->second;
}
private:
int id_;
vector<map<int, int>> vals_;
};
/**
* 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);
*/