forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
64 lines (47 loc) · 1.38 KB
/
main2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// Source : https://leetcode.com/problems/time-based-key-value-store/
/// Author : liuyubobobo
/// Time : 2019-01-28
#include <iostream>
#include <unordered_map>
#include <map>
using namespace std;
/// Using TreeMap to store (time, value) pair information for each key
/// Time Complexity: init: O(1)
/// set: O(logn)
/// get: O(logn)
/// Space Complexity: O(n)
class TimeMap {
private:
unordered_map<string, map<int, string>> timeMap; // key -> (time, value)
public:
/** Initialize your data structure here. */
TimeMap() {}
void set(string key, string value, int timestamp) {
timeMap[key][timestamp] = value;
}
string get(string key, int timestamp) {
map<int, string>::iterator iter =
timeMap[key].lower_bound(timestamp);
if(iter == timeMap[key].end() || iter->first > timestamp){
if(iter == timeMap[key].begin()) return "";
iter --;
}
return iter->second;
}
};
int main() {
TimeMap timeMap;
timeMap.set("foo", "bar", 1);
cout << timeMap.get("foo", 1) << endl;
// bar
cout << timeMap.get("foo", 3) << endl;
// bar
timeMap.set("foo", "bar2", 4);
cout << timeMap.get("foo", 4) << endl;
// bar2
cout << timeMap.get("foo", 5) << endl;
// bar2
cout << timeMap.get("foo", 1) << endl;
// bar
return 0;
}