• [python 刷题] 981 Time Based Key-Value Store


    [python 刷题] 981 Time Based Key-Value Store

    题目:

    Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key’s value at a certain timestamp.

    Implement the TimeMap class:

    • TimeMap() Initializes the object of the data structure.
    • void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp.
    • String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns "".

    这是一道设计题,LC 提供的 boilerplate 为:

    class TimeMap:
    
        def __init__(self):
    
    
        def set(self, key: str, value: str, timestamp: int) -> None:
    
    
        def get(self, key: str, timestamp: int) -> str:
    
    
    
    # Your TimeMap object will be instantiated and called as such:
    # obj = TimeMap()
    # obj.set(key,value,timestamp)
    # param_2 = obj.get(key,timestamp)```
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在开始实现设计题之前,首先要确认应该选择什么数据结构,而这一点题目中也有提示:根据 key 获得对应的数据

    也就是说,这里肯定是有 hash table 的一席之地。至于 hash table 里面要存储的是什么数据格式,这个可以从 constraint 里面看到:

    • All the timestamps timestamp of set are strictly increasing.

    也就是说,每次调用的 timestamp 都是持续增长的,也就是排序的。一个已经排好序的数据结构+搜索的题型,不出意外的就会联想到 binary search

    也因此,初始化的数据结构可以定位 collections.defaultdict(list)

    使用 defaultdict 的原因是因为处理空值方便一些

    对于 set 这个方法来说,保存进数组的格式就没有这么严格了,只要能够获取时间戳和值就行。题目中已经提到了会严格增长,也就是说不会重复,那么使用 dict 也可以,这里就使用另一个数组了

    最后就是 get,算是一个比较常规的寻找比 n n n 小的最大数字这种 binary search,也就是说:

    • 找到 timestamp 时返回对应的数字

    • 当前数字比 timestamp 大时, [ l , . . . , m − 1 ] [l, ..., m - 1] [l,...,m1] 继续开始搜索

    • 当前数字比 timestamp 小时, [ m + 1 , . . . , r ] [m + 1, ..., r] [m+1,...,r] 继续开始搜索

      同时为了防止等同时间戳的数字不存在,当前值为最贴近时间戳的值,更新 res 使得终止循环时可以返回当前值

    完整代码如下:

    class TimeMap:
    
        def __init__(self):
            self.d = collections.defaultdict(list)
    
    
        def set(self, key: str, value: str, timestamp: int) -> None:
            self.d[key].append([value, timestamp])
    
    
        def get(self, key: str, timestamp: int) -> str:
            vals = self.d[key]
            l, r = 0,len(vals) - 1
            res = ''
            while l <= r:
                m = (l + r) // 2
                if vals[m][1] == timestamp:
                    return vals[m][0]
    
                if vals[m][1] < timestamp:
                    res = vals[m][0]
                    l = m + 1
                else:
                    r = m - 1
    
            return res
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    一个信号间相互干扰问题的发现及解决方法
    引流、变现、留存解决方案—“消费”+“分享”的聚合生态-分享购
    【ELK】日志系统&部署
    【考研】数据结构考点——堆排序(含408真题)
    苹果前工程师张晓浪承认窃取汽车商业机密,或被判10年监禁
    宝妈刷单被骗125万元,我们该如何避免被骗?
    字节与字符与常见编码方式
    高通导航器软件开发包使用指南(16)
    每天一个数据分析题(四百零一)- 逻辑回归
    【无标题】
  • 原文地址:https://blog.csdn.net/weixin_42938619/article/details/133636891