forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0981-time-based-key-value-store.cs
52 lines (43 loc) · 1.36 KB
/
0981-time-based-key-value-store.cs
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
public class TimeMap {
private Dictionary<string, List<(int timestamp, string value1)>> _dict;
public TimeMap() {
_dict = new Dictionary<string, List<(int, string)>>();
}
public void Set(string key, string value, int timestamp) {
var value1 = new List<(int, string)>();
if(!_dict.ContainsKey(key)){
_dict.Add(key, value1);
}
_dict[key].Add((timestamp, value));
}
public string Get(string key, int timestamp) {
if(!_dict.ContainsKey(key)){
return "";
}
var value = _dict[key];
var left = 0;
var right = value.Count;
var result = "";
while(left < right){
var mid = (left + right)/2;
if(value[mid].timestamp == timestamp){
result = value[mid].value1;
return result;
}
else if(value[mid].timestamp < timestamp){
left = mid + 1;
result = value[mid].value1;
}
else{
right = mid;
}
}
return result;
}
}
/**
* 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);
*/