Tensorum
YouTubeশেখা শুরু করুন
LeetCode 981 · Design

Time Based Key-Value Store

LeetCode 981 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Per-key sorted list + binary search for floor(timestamp).

মাঝারিHash TableStringBinary SearchDesign
LeetCode-এ খুলুন ↗

আমি Time Based Key-Value Store (LeetCode 981) এভাবে সমাধান করেছি — Design প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

For each key, store (timestamp, value) pairs in the order they arrive — timestamps only increase, so the list is sorted. A get is then a binary search for the latest timestamp ≤ the query.

Approach

  1. 1

    set: append [timestamp, value] to the key's list.

  2. 2

    get: if the key is missing or the query is before the first timestamp, return ''.

  3. 3

    Binary search; on a smaller timestamp remember its value and go right.

  4. 4

    Return the remembered value (or the exact match).

আমি কীভাবে সমাধান করেছি

Hash map + binary searchPythonO(log n) get
class TimeMap:

    def __init__(self):
        self.time_key_map= defaultdict(list)

    def set(self, key: str, value: str, timestamp: int) -> None:
        self.time_key_map[key].append([timestamp,value])

    def get(self, key: str, timestamp: int) -> str:
        if key not in self.time_key_map:
            return ""
        if timestamp<self.time_key_map[key][0][0]:
            return ""
        low = 0
        high = len(self.time_key_map[key])-1
        res=""
        while low<=high:
            mid = (low+high)//2
            mid_time_stamp_key =  self.time_key_map[key][mid][0]
            if mid_time_stamp_key==timestamp:
                return self.time_key_map[key][mid][1]
            elif mid_time_stamp_key>timestamp:
                high= mid-1
            else:
                low = mid+1
                res=self.time_key_map[key][mid][1]
        return res
        


# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
স্টেপ-থ্রু লোড হচ্ছে…

Time Based Key-Value Store — সাধারণ প্রশ্ন

Time Based Key-Value Store-এর পেছনে intuition কী?

For each key, store (timestamp, value) pairs in the order they arrive — timestamps only increase, so the list is sorted. A get is then a binary search for the latest timestamp ≤ the query.

Time Based Key-Value Store (LeetCode 981) কীভাবে সমাধান করবেন?

1. set: append [timestamp, value] to the key's list. 2. get: if the key is missing or the query is before the first timestamp, return ''. 3. Binary search; on a smaller timestamp remember its value and go right. 4. Return the remembered value (or the exact match).

Time Based Key-Value Store-এর time আর space complexity কত?

Time: O(1) set, O(log n) get। Space: O(n)। (Accepted approach: Hash map + binary search।)

Time Based Key-Value Store-এ সাধারণ ভুলগুলো কী?

Remember the candidate when going right — that's the floor.