Skip to content

Commit c61d805

Browse files
authored
981. Time Based Key-Value Store
1 parent dd653e4 commit c61d805

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class TimeMap {
2+
unordered_map<string, vector<pair<int, string>>> db;
3+
public:
4+
TimeMap() {
5+
6+
}
7+
8+
void set(string key, string value, int timestamp) {
9+
db[key].push_back({timestamp, value});
10+
}
11+
12+
string get(string key, int timestamp) {
13+
auto cmp = [](const pair<int, string>& lhs, const pair<int, string>& rhs) {
14+
return lhs.first < rhs.first;
15+
};
16+
auto it = upper_bound(db[key].begin(), db[key].end(), make_pair(timestamp, ""), cmp);
17+
if (it == db[key].begin()) {
18+
return "";
19+
} else {
20+
it--;
21+
return it->second;
22+
}
23+
}
24+
};
25+
26+
/**
27+
* Your TimeMap object will be instantiated and called as such:
28+
* TimeMap* obj = new TimeMap();
29+
* obj->set(key,value,timestamp);
30+
* string param_2 = obj->get(key,timestamp);
31+
*/

0 commit comments

Comments
 (0)