Design a time-based key-value store. set(key, value, timestamp) stores a value; get(key, timestamp) returns the value with the largest stored timestamp ≤ timestamp, or "". Timestamps passed to set are strictly increasing.
Each key's history is already sorted by timestamp. Binary search for the last timestamp ≤ the query.
1class TimeMap {2store = new Map<string, [number, string][]>();3set(key: string, value: string, timestamp: number): void {4if (!this.store.has(key)) this.store.set(key, []);5this.store.get(key)!.push([timestamp, value]);6}7get(key: string, timestamp: number): string {8const hist = this.store.get(key) ?? [];9let lo = 0, hi = hist.length - 1, ans = "";10while (lo <= hi) {11const mid = (lo + hi) >> 1;12if (hist[mid][0] <= timestamp) { ans = hist[mid][1]; lo = mid + 1; }13else hi = mid - 1;14}15return ans;16}17}
Empty store.
Space: play/pause · ←/→: step