-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime-Based-Key-Value-Store.cpp
More file actions
57 lines (52 loc) · 1.31 KB
/
Time-Based-Key-Value-Store.cpp
File metadata and controls
57 lines (52 loc) · 1.31 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
class TimeMap
{
public:
/** Initialize your data structure here. */
struct node
{
/* data */
string val;
int timestamp;
node(){};
node(string str, int tstp) : val(str), timestamp(tstp){};
};
unordered_map<string, vector<node>> m;
TimeMap(){};
void set(string key, string value, int timestamp)
{
auto iter = m.find(key);
if (iter == m.end())
{
m.insert(make_pair(key, vector<node>()));
iter = m.find(key);
};
(iter->second).push_back(node(value, timestamp));
}
string get(string key, int timestamp)
{
auto iter = m.find(key);
if (iter == m.end())
return "";
else
{
auto it = (iter->second).rbegin();
while (it != (iter->second).rend())
{
if (it->timestamp > timestamp)
it++;
else
break;
};
return it == (iter->second).rend() ? "" : it->val;
};
return "";
}
};
/**
* 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);
*/