LeetCode 981. Time Based Key-Value Store

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class TimeMap {
unordered_map<string, vector<pair<int, string>>> m;
public:
TimeMap() {}
void set(string key, string value, int timestamp) {
m[key].emplace_back(timestamp, value);
}
string get(string key, int timestamp) {
auto &v = m[key];
pair<int, string> p(timestamp, string({127}));
auto it = upper_bound(v.begin(), v.end(), p);
return it == v.begin() ? "" : (it - 1)->second;
}
};