Skip to content

Commit a0c6dd2

Browse files
Create 981-Time-Based-Key-Value-Store.cs
File(s) Modified: 981-Time-Based-Key-Value-Store.cs Language(s) Used: csharp Submission URL: https://leetcode.com/submissions/detail/754743169/
1 parent 341ddaa commit a0c6dd2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
public class TimeMap {
2+
3+
private Dictionary<string, List<(int timestamp, string value1)>> _dict;
4+
public TimeMap() {
5+
_dict = new Dictionary<string, List<(int, string)>>();
6+
}
7+
8+
public void Set(string key, string value, int timestamp) {
9+
var value1 = new List<(int, string)>();
10+
if(!_dict.ContainsKey(key)){
11+
_dict.Add(key, value1);
12+
}
13+
_dict[key].Add((timestamp, value));
14+
15+
}
16+
17+
public string Get(string key, int timestamp) {
18+
if(!_dict.ContainsKey(key)){
19+
return "";
20+
}
21+
var value = _dict[key];
22+
23+
var left = 0;
24+
var right = value.Count;
25+
var result = "";
26+
27+
while(left < right){
28+
var mid = (left + right)/2;
29+
if(value[mid].timestamp == timestamp){
30+
result = value[mid].value1;
31+
return result;
32+
}
33+
else if(value[mid].timestamp < timestamp){
34+
left = mid + 1;
35+
result = value[mid].value1;
36+
}
37+
else{
38+
right = mid;
39+
}
40+
41+
}
42+
43+
return result;
44+
}
45+
}
46+
47+
/**
48+
* Your TimeMap object will be instantiated and called as such:
49+
* TimeMap obj = new TimeMap();
50+
* obj.Set(key,value,timestamp);
51+
* string param_2 = obj.Get(key,timestamp);
52+
*/

0 commit comments

Comments
 (0)