1
var TimeMap = function () {7
* @param {string} value8
* @param {number} timestamp11
TimeMap.prototype.set = function (key, value, timestamp) {12
if (!this.data.has(key)) {13
this.data.set(key, [{ timestamp: timestamp, value: value }]);15
let temp_store = this.data.get(key);16
temp_store.push({ timestamp: timestamp, value: value });17
this.data.set(key, temp_store);23
* @param {number} timestamp26
TimeMap.prototype.get = function (key, timestamp) {27
if (this.data.has(key)) {28
const keyArray = this.data.get(key);30
//Optimize with binary search - Ordered by insert time, O(log n) devide and conq method (Like searching a dictionary)31
const index = keyArray.binarySearch(timestamp);32
if (keyArray[index].timestamp > timestamp) {36
return keyArray[index].value || prev;42
Array.prototype.binarySearch = function (key) {44
let right = this.length - 1;46
while (left < right) {47
const i = Math.floor((left + right + 1) / 2);48
if (this[i].timestamp > key) {59
* Your TimeMap object will be instantiated and called as such:60
* var obj = new TimeMap()61
* obj.set(key,value,timestamp)62
* var param_2 = obj.get(key,timestamp)