-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathTime Based Key-Value Store.js
65 lines (54 loc) · 1.59 KB
/
Time Based Key-Value Store.js
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
65
var TimeMap = function() {
this.data = new Map();
};
/**
* @param {string} key
* @param {string} value
* @param {number} timestamp
* @return {void}
*/
TimeMap.prototype.set = function(key, value, timestamp) {
if(!this.data.has(key)){
this.data.set(key, [{timestamp: timestamp, value: value}])
} else {
let temp_store = this.data.get(key);
temp_store.push({timestamp: timestamp, value: value});
this.data.set(key, temp_store);
}
};
/**
* @param {string} key
* @param {number} timestamp
* @return {string}
*/
TimeMap.prototype.get = function(key, timestamp) {
if(this.data.has(key)){
const keyArray = this.data.get(key);
//Optimize with binary search - Ordered by insert time, O(log n) devide and conq method (Like searching a dictionary)
const index = keyArray.binarySearch(timestamp);
if(keyArray[index].timestamp > timestamp){
return ''
}
return keyArray[index].value || prev;
}
return '';
};
Array.prototype.binarySearch = function(key){
let left = 0;
let right = this.length - 1;
while (left < right) {
const i = Math.floor((left + right + 1) / 2);
if (this[i].timestamp > key) {
right = i - 1;
} else {
left = i;
}
}
return left;
}
/**
* Your TimeMap object will be instantiated and called as such:
* var obj = new TimeMap()
* obj.set(key,value,timestamp)
* var param_2 = obj.get(key,timestamp)
*/