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. - 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 timestampprev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestampprev. If there are no such values, it returns "".
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"]
Topics: binary-search, hash-map
Asked by: Netflix, Google, Amazon, Stripe
Time complexity: O(log n) per get. Space complexity: O(n).