Pythonic Implementations of Time Based Key-Val Store
Posted onViews: Word count in article: 279Reading time ≈1 mins.
Approach 1: Linear Search
Our first solution is using dictionary. This has a O(1) complexity for setting and O(n) for getting, as we have to perform linear search. There are two tricks:
Extending the defaultdict class.
Chaining the .get(key, {}).items() expressions to make code looks more elegant.
defget(self, key: str, timestamp: int) -> str: max_time = 0 res = "" for time, val insuper().get(key, {}).items(): if max_time < time <= timestamp: max_time = time res = val return res
However, the runtime exceeded limit when testing on LeetCode.
Approach 2: Binary Search
Alternatively, we can put time: val pairs into list, and use binary search to find them.
defget(self, key: str, timestamp: int) -> str: index = bisect_right(super().get(key, []), timestamp, key=lambda x: x[0]) returnself[key][index - 1][1] if index else""
Notice that the same trick here is used again. When the list is empty, bisect_right returns 0, which again indicates there’s not valid query result. With the short circuit evaluation, self[key] won’t be executed either.