981. Time Based Key Value Store - cocoder39/coco39_LC GitHub Wiki

981. Time Based Key-Value Store

class TimeMap:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.cache = collections.defaultdict(list)

    def set(self, key: str, value: str, timestamp: int) -> None:
        self.cache[key].append((timestamp, value))

    def get(self, key: str, timestamp: int) -> str:
        values = self.cache[key]
        idx = bisect.bisect_right(values, (timestamp+1, ))
        return values[idx-1][1] if idx else ""