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.