File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments