forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-based-key-value-store.cpp
40 lines (33 loc) · 1.1 KB
/
time-based-key-value-store.cpp
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
40
// Time: set: O(1)
// get: O(logn)
// Space: O(n)
class TimeMap {
public:
/** Initialize your data structure here. */
TimeMap() {
}
void set(string key, string value, int timestamp) {
lookup_[key].emplace_back(timestamp, value);
}
string get(string key, int timestamp) {
if (!lookup_.count(key)) {
return "";
}
auto it = upper_bound(lookup_[key].cbegin(),
lookup_[key].cend(),
make_pair(timestamp + 1, ""),
[](const pair<int, string>& lhs,
const pair<int, string>& rhs) {
return lhs.first < rhs.first;
});
return it != lookup_[key].cbegin() ? prev(it)->second : "";
}
private:
unordered_map<string, vector<pair<int, string>>> lookup_;
};
/**
* 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);
*/