Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.

Implement the TimeMap class:


	TimeMap() Initializes the object of the data structure.
	void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp.
	String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev &lt;= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns "".


&nbsp;
Example 1:

Input
["TimeMap", "set", "get", "get", "set", "get", "get"]
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
Output
[null, null, "bar", "bar", null, "bar2", "bar2"]

Explanation
TimeMap timeMap = new TimeMap();
timeMap.set("foo", "bar", 1);  // store the key "foo" and value "bar" along with timestamp = 1.
timeMap.get("foo", 1);         // return "bar"
timeMap.get("foo", 3);         // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar".
timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4.
timeMap.get("foo", 4);         // return "bar2"
timeMap.get("foo", 5);         // return "bar2"


&nbsp;
Constraints:


	1 &lt;= key.length, value.length &lt;= 100
	key and value consist of lowercase English letters and digits.
	1 &lt;= timestamp &lt;= 107
	All the timestamps timestamp of set are strictly increasing.
	At most 2 * 105 calls will be made to set and get.


class TimeMap:

    def __init__(self):
        self.dict = {}
        

    def set(self, key: str, value: str, timestamp: int) -> None:
        if key not in self.dict:
            self.dict[key] = ([], [])
            self.dict[key][0].append(value)
            self.dict[key][1].append(timestamp)
        else:
            self.dict[key][0].append(value)
            self.dict[key][1].append(timestamp)
    
    
    def bsearch(self, nums, target):
        beg = 0
        end = len(nums)-1
        lastIndex = len(nums)-1
        
        while beg<=end:
            mid = (beg+end)//2
            if target == nums[mid]:
                return mid
            elif target < nums[mid]:
                end = mid-1
            elif target > nums[mid]:
                beg = mid+1
        
        
        if target < nums[mid] and mid == 0:
            return -1
        if target > nums[mid]:
            return mid
        return mid-1
            
    def get(self, key: str, timestamp: int) -> str:
        if key not in self.dict:
            return ""
        
        index = self.bsearch(self.dict[key][1], timestamp)
        return self.dict[key][0][index] if 0 <= index < len(self.dict[key][0]) else ""
    


# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
class TimeMap {
    private Map<String, List<Entry>> map;
    private final String NOT_FOUND = "";
    public TimeMap() {
        map = new HashMap<>();
    }
    
    public void set(String key, String value, int timestamp) {
        List<Entry> entries = map.getOrDefault(key, new ArrayList<>());
        entries.add(new Entry(value, timestamp));
        map.put(key, entries);
    }
    
    public String get(String key, int timestamp) {
        List<Entry> entries = map.get(key);
        if (entries == null) {
            return NOT_FOUND;
        }
        return binarySearch(entries, timestamp);
    }
    
    private String binarySearch(List<Entry> entries, int timestamp) {
        int lo = 0, hi = entries.size() - 1, mid = -1;
        String ans = "";
        
        // Base cases - if value is not set, return empty
        if (entries.get(lo).timestamp > timestamp) {
            return NOT_FOUND;
        }
        // If timestamp is equal or greater, return the last value saved in map against this key, since that will have the largest timestamp
        else if (entries.get(hi).timestamp <= timestamp) {
            return entries.get(hi).value;
        }
        
        // Else apply binary search to get correct value
        while (lo <= hi) {
            mid = lo + (hi-lo)/2;
            Entry entry = entries.get(mid);
            // System.out.println("mid: "+mid);
            if (entry.timestamp == timestamp) {
                return entry.value;
            }
            // Save ans, and look for ans on right half to find greater timestamp
            else if (entry.timestamp < timestamp) {
                ans = entry.value;
                lo = mid + 1;
            }
            else {
                hi = mid - 1;
            }
        }
        return ans;
    }
}

class Entry {
    String value;
    int timestamp;
    
    public Entry(String value, int timestamp) {
        this.value = value;
        this.timestamp = timestamp;
    }
}
class TimeMap {
public:
    map<string,vector<pair<int,string>>> mp;
    string Max;
    TimeMap() {
        Max = "";
    }
    
    void set(string key, string value, int timestamp) {
        mp[key].push_back(make_pair(timestamp,value));
        Max = max(Max,value);
    }
    
    string get(string key, int timestamp) {
        pair<int,string> p = make_pair(timestamp,Max);
        int l = upper_bound(mp[key].begin(),mp[key].end(),p)-mp[key].begin();
        if(l==0){
            return "";
        }
        return mp[key][l-1].second;
    }
};

/**
 * Your TimeMap object will be instantiated and called as such:
 * TimeMap* obj = new TimeMap();
 * obj->set(key,value,timestamp);
 * string param_2 = obj->get(key,timestamp);
 */
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)
 */
Time Based Key-Value Store