【LeetCode】981. Time Based Key-Value Store 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/time-based-key-value-store/
题目描述
Create a timebased key-value store class TimeMap
, that supports two operations.
set(string key, string value, int timestamp)
- Stores the
key
andvalue
, along with the given timestamp.
get(string key, int timestamp)
- Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
- If there are multiple such values, it returns the one with the largest timestamp_prev.
- If there are no values, it returns the empty string (
""
).
Example 1:
Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
Output: [null,null,"bar","bar",null,"bar2","bar2"]
Explanation:
TimeMap kv;
kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1
kv.get("foo", 1); // output "bar"
kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"
kv.set("foo", "bar2", 4);
kv.get("foo", 4); // output "bar2"
kv.get("foo", 5); //output "bar2"
Example 2:
Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]]
Output: [null,null,null,"","high","high","low","low"]
Note:
- All key/value strings are lowercase.
- All key/value strings have length in the range [1, 100]
- The timestamps for all TimeMap.set operations are strictly increasing.
- 1 <= timestamp <= 10^7
- TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.
题目大意
构建一个带有时间版本的KV存储器。即每次保存的时候会保存当前的时间,查询的时候给出一个时间,要求找到先于该时间的最新的key对应的value。
解题方法
字典
没想到LC还会出这么新颖的题目,这个应用场景在数据库设计中确实会用到。
首先分析一下,我们应该怎么办。首先,如果给出了任意的时间,我们都要找出先于这个时间的key对应的value,说明我们必须把每次的插入结果与对应的时间都保存,而不能使用覆盖的方式。
很显然我们会使用字典这种数据结构来存储kv对,为了保存每个key插入的时间,而且要保证最快的查询时间,我分开保存每次插入的key的time和value。为什么这么做有效呢?因为如果我如果使用key : [(time1, value1), (time2, value2)...]
这种存储方式,对快速查找是不利的。而使用key : [time1, time2...]
,key : [value1, value2...]
这种存储方式能保证time和value是一一对应的。所以这种方式先根据key和time快速查找到小于该时间的timex,然后就能根据索引快速找到此索引对应的valuex.
在有序列表中快速查找小于一个time的time_x,当然使用二分了,所以使用了bisect_right来快速查找到了一个不大于该time的时间对应的索引,然后拿这个索引找到对应的value即可。
万万没想到的是,竟然没通过!我已经优化了存储和查找啊!看了下没通过的测试用例,发现是一个键值对反复的插入查找,每次查找的时间都是最新的时间。好吧,那么我使用了一个这个max_字典,用来保存当前的key更新的时间。这样的好处是,当我们查找一个不小于当前时间的值的时候,一定是最后一次插入的那个时间。
最后,说句题外话,这个题没有考到的是我们的删除操作怎么办?其实业界做法是使用延迟删除的方式,即插入一个标志位代表删除,而不进行真正的删除操作。比如我们在最后的时刻设置key为"",这样我们查找的时候发现这个key的数值是""就意味着被删除。
class TimeMap(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.t_ = collections.defaultdict(list)
self.v_ = collections.defaultdict(list)
self.max_ = collections.defaultdict(int)
def set(self, key, value, timestamp):
"""
:type key: str
:type value: str
:type timestamp: int
:rtype: None
"""
self.t_[key].append(timestamp)
self.v_[key].append(value)
self.max_[key] = max(self.max_[key], timestamp)
def get(self, key, timestamp):
"""
:type key: str
:type timestamp: int
:rtype: str
"""
if key not in self.t_:
return ""
if timestamp >= self.max_[key]:
return self.v_[key][-1]
v = bisect.bisect_right(self.t_[key], timestamp)
if v:
return self.v_[key][v - 1]
return ""
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
日期
2019 年 1 月 27 日 —— 这个周赛不太爽
【LeetCode】981. Time Based Key-Value Store 解题报告(Python)的更多相关文章
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- Leetcode 981. Time Based Key-Value Store(二分查找)
题目来源:https://leetcode.com/problems/time-based-key-value-store/description/ 标记难度:Medium 提交次数:1/1 代码效率 ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
随机推荐
- EXCEL-REPLACE()替换字符串最后几位 删除字符串最后几位
字符串 0M5(烈焰红) 我要删除最后一个字符")" 公式=REPLACE(ASC(字符串),LEN(ASC(字符串)),1,"") 解释:=REPLAC ...
- kubernetes部署 flannel网络组件
创建 flannel 证书和私钥flannel 从 etcd 集群存取网段分配信息,而 etcd 集群启用了双向 x509 证书认证,所以需要为 flanneld 生成证书和私钥. cat > ...
- 日常Java 2021/10/14
Java数据结构 Java BitSet类 BitSet类创建一种特殊类型的数组来保存位值,数组大小随需要增加,BitSet(),BitSet(int size) 其中的方法 void and(Bit ...
- Mapreduce中的join操作
一.背景 MapReduce提供了表连接操作其中包括Map端join.Reduce端join还有半连接,现在我们要讨论的是Map端join,Map端join是指数据到达map处理函数之前进行合并的,效 ...
- Java 性能优化的 50 个细节
在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. #尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间, ...
- LinkBinTree
package ch11; import java.util.ArrayList; import java.util.List; import java.util.Stack; public clas ...
- go channel 概述
精髓 将资源读进内存-->共享内存,一个个进程/线程进行处理,这是常见模式.go channel 是一种直接在进程/线程之间传递资源的方式,即以通信来共享内存.这便是go的精髓. 扩展-一些名词 ...
- 阿里云esc 安装 mysql5.7.27
1. 下载: wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 2. 安装: (1) yum -y in ...
- html之table的tr加间隔
<table style="border-collapse:separate; border-spacing:0px 10px;"> <tr> <td ...
- 【C/C++】金币
做了一下去年的题目,今年看起来就没这么难了 从上到下的可以从下到上考虑,会简单很多,dp入门 题目 金币 小招在玩一款游戏,在一个N层高的金字塔上,以金字塔顶为第一层,第i层有i个落点,每个落点有若干 ...