-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeMap.cpp
More file actions
34 lines (31 loc) · 964 Bytes
/
TimeMap.cpp
File metadata and controls
34 lines (31 loc) · 964 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 TimeMap {
public:
TimeMap() {
m.clear();
}
void set(string key, string value, int timestamp) {
m[key].push_back({timestamp, value});
}
string get(string key, int timestamp) {
vector<pair<int, string> > &v = m[key];
if (!v.size() || timestamp < v.begin()->first) {
return "";
}
if (timestamp >= v.back().first) {
return v.back().second;
}
pair<int, string> p = {timestamp, ""};
auto it = std::upper_bound(v.begin(), v.end(), p, [](const pair<int, string> &a, const pair<int, string> &b) {
return a.first < b.first;
});
return (*prev(it)).second;
}
private:
unordered_map<string, vector<pair<int, string>> > m;
};
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap* obj = new TimeMap();
* obj->set(key,value,timestamp);
* string param_2 = obj->get(key,timestamp);
*/