Studied about algorithms and data structures
Time Based Key-Value Store

#binarysearch #100daysleetcode

How to solve:
  • create a dictionary to store the key with data values
  • return largest timestamp_prev -> do binary search

class TimeMap:

    def __init__(self):
        self.store = {} #key: list of [val, timestap]

    def set(self, key: str, value: str, timestamp: int) -> None:
        if key not in self.store:
            self.store[key] = []
        self.store[key].append([value, timestamp])

    def get(self, key: str, timestamp: int) -> str:
        result = ''
        values = self.store.get(key, [])
        
        # binary search
        
        left, right = 0, len(values) - 1

        while left <= right:
            mid = (left+right) // 2
            
            if values[mid][1] <= timestamp:
                result = values[mid][0]
                left = mid + 1
            else:
                right = mid -1
                
        return result