Faaez Razeen

Time Based Key-Value Store

  • 2 min read
  • LC-Medium
  • Binary-search

3 years ago

Solution

TimeSpaceExplanation
O(log n)O(1)
class TimeMap: def __init__(self): self.store = defaultdict(list) def set(self, key: str, value: str, timestamp: int) -> None: self.store[key].append((value, timestamp)) def get(self, key: str, timestamp: int) -> str: ans = '' values = self.store[key] l, r = 0, len(values) - 1 while l <= r: mid = (l + r) // 2 if values[mid][1] <= timestamp: ans = values[mid][0] l = mid + 1 else: r = mid - 1 return ans