Advertisement
smj007

Untitled

Mar 5th, 2024
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. class TimeMap:
  2.  
  3.     def __init__(self):
  4.         self.hashmap = defaultdict(list)
  5.  
  6.     def set(self, key: str, value: str, timestamp: int) -> None:
  7.         self.hashmap[key].append([value, timestamp])
  8.        
  9.     def get(self, key: str, timestamp: int) -> str:
  10.        
  11.         if key not in self.hashmap:
  12.             return ""
  13.         ans = ""
  14.         store = self.hashmap[key]
  15.         low, high = 0, len(store) - 1
  16.  
  17.         while low <= high:
  18.             mid = (low + high)//2
  19.             if store[mid][1] <= timestamp:
  20.                 ans = store[mid][0]
  21.                 low = mid + 1
  22.             else:
  23.                 high = mid - 1
  24.            
  25.         return ans
  26.  
  27.  
  28. # Your TimeMap object will be instantiated and called as such:
  29. # obj = TimeMap()
  30. # obj.set(key,value,timestamp)
  31. # param_2 = obj.get(key,timestamp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement