Advertisement
DeepRest

981. Time Based Key Value Store

Jun 20th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. class TimeMap:
  2. def __init__(self):
  3. self.hashMap = {}
  4.  
  5. def set(self, key, value, timestamp):
  6. self.hashMap.setdefault(key, []).append((timestamp, value))
  7.  
  8. def get(self, key, timestamp):
  9. arr = self.hashMap[key]
  10. left = 0
  11. right = len(arr) - 1
  12. mid = (left + right)//2
  13.  
  14. if timestamp < arr[0][0]:
  15. return ''
  16. while(True):
  17. if left > right:
  18. return arr[right][1]
  19. if arr[mid][0] == timestamp:
  20. return arr[mid][1]
  21.  
  22. if timestamp < arr[mid][0]:
  23. right = mid -1
  24. else:
  25. left = mid + 1
  26. mid = (left + right)//2
  27.  
  28. obj = TimeMap()
  29. obj.set('love', 'high', 10)
  30. obj.set('love', 'low', 20)
  31. print(obj.get('love', 5))
  32. print(obj.get('love', 10))
  33. print(obj.get('love', 15))
  34. print(obj.get('love', 20))
  35. print(obj.get('love', 25))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement