Design a class SettingsLog that remembers every value a setting has ever had and can answer what the
setting was at any moment:
SettingsLog()starts empty.set(key, value, t)records thatkeytook the valuevalueat timet. Across allsetcalls the times are strictly increasing.get(key, t)returns the value recorded forkeywith the largest time that is<= t. Ifkeywas never set, or was only set aftert, return the empty string"".
Examples
ops: ["SettingsLog", "set", "get", "get", "set", "get", "get", "get"]
args: [[], ["theme", "dark", 5], ["theme", 4], ["theme", 5], ["theme", "light", 9], ["theme", 8], ["theme", 12], ["font", 12]]
Output: [None, None, "", "dark", None, "dark", "light", ""]
Explanation: at time 4 the theme had not been set yet; at 8 it was still "dark"; "font" was never set.
Constraints
- Keys are short strings; values are strings or integers;
0 <= t <= 10**9 - Up to
10**4calls in total;gettimes are arbitrary (not necessarily increasing) - Target:
setinO(1)amortised,getinO(log h)wherehis the number of values stored for that key
Goals
- Keep a sorted history per key so that past values can be looked up
- Answer 'value as of time t' with binary search instead of a scan