@@ -16,6 +16,56 @@ TimeMap.prototype.set = function(key, value, timestamp) {
16
16
this . hash [ key ] . push ( [ value , timestamp ] )
17
17
} ;
18
18
19
+ /**
20
+ * @param {string } key
21
+ * @param {number } timestamp
22
+ * @return {string }
23
+ */
24
+ TimeMap . prototype . get = function ( key , timestamp ) {
25
+ if ( this . hash [ key ] == null ) return ''
26
+ const arr = this . hash [ key ]
27
+
28
+ let l = 0 , r = arr . length - 1 ;
29
+ while ( l <= r ) {
30
+ const pick = Math . floor ( ( l + r ) / 2 ) ;
31
+ if ( arr [ pick ] [ 1 ] < timestamp ) {
32
+ l = pick + 1 ;
33
+ } else if ( arr [ pick ] [ 1 ] > timestamp ) {
34
+ r = pick - 1
35
+ } else {
36
+ return arr [ pick ] [ 0 ] ;
37
+ }
38
+ }
39
+ return arr [ r ] ?. [ 0 ] || ''
40
+ } ;
41
+
42
+ /**
43
+ * Your TimeMap object will be instantiated and called as such:
44
+ * var obj = new TimeMap()
45
+ * obj.set(key,value,timestamp)
46
+ * var param_2 = obj.get(key,timestamp)
47
+ */
48
+
49
+ // another
50
+
51
+ /**
52
+ * Initialize your data structure here.
53
+ */
54
+ const TimeMap = function ( ) {
55
+ this . hash = { }
56
+ } ;
57
+
58
+ /**
59
+ * @param {string } key
60
+ * @param {string } value
61
+ * @param {number } timestamp
62
+ * @return {void }
63
+ */
64
+ TimeMap . prototype . set = function ( key , value , timestamp ) {
65
+ if ( this . hash [ key ] == null ) this . hash [ key ] = [ ]
66
+ this . hash [ key ] . push ( [ value , timestamp ] )
67
+ } ;
68
+
19
69
/**
20
70
* @param {string } key
21
71
* @param {number } timestamp
0 commit comments