Time Based Key-Value Store
MediumBinary Search
Design a class TimeMap that stores multiple values for the same key at different times and can answer "what was the value of this key at time t?".
TimeMap()creates an empty store.set(key, value, timestamp)records thatkeyhadvaluestarting attimestamp. Earlier values for the key are kept, not overwritten.get(key, timestamp)returns the value from thesetcall forkeywith the largest timestamp that is<= timestamp. Ifkeywas never set, or every write for it is later thantimestamp, return the empty string"".
Timestamps passed to set are strictly increasing across all calls, so each key's history arrives already sorted by time.
Example 1
- Input:
- TimeMap(); set("price", "10", 2); set("price", "12", 5); get("price", 4); get("price", 5); get("price", 1)
- Output:
- [null,null,null,"10","12",""]
- Explanation
- At time 4 the latest write at or before 4 is the one at time 2, so
"10". At time 5 the write at 5 counts (the match is inclusive), giving"12". Nothing was written at or before time 1, so that read returns"".
Example 2
- Input:
- TimeMap(); set("a", "x", 3); set("b", "y", 7); get("a", 9); get("b", 6)
- Output:
- [null,null,null,"x",""]
- Explanation
- Keys are independent.
awas last written at 3, so a read at 9 returns"x".bwas only written at 7, which is after 6, so that read returns"".
Constraints
1 <= key.length, value.length <= 100keyandvalueconsist of lowercase English letters and digits1 <= timestamp <= 10^7- Timestamps of
setcalls are strictly increasing - At most
2 * 10^5calls in total tosetandget