LeetCode 981. Time Based Key-Value Store
原题链接在这里:https://leetcode.com/problems/time-based-key-value-store/
题目:
Create a timebased key-value store class TimeMap, that supports two operations.
1. set(string key, string value, int timestamp)
- Stores the
keyandvalue, along with the giventimestamp.
2. get(string key, int timestamp)
- Returns a value such that
set(key, value, timestamp_prev)was called previously, withtimestamp_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
timestampsfor allTimeMap.setoperations are strictly increasing. 1 <= timestamp <= 10^7TimeMap.setandTimeMap.getfunctions will be called a total of120000times (combined) per test case.
题解:
For the same key, if timestamp is different, value could be different.
There could be cases <key, value1> with timestamp1, <key, value2> with timestamp2. Both values are stored. The second value is NOT overriding first value.
Have a HashMap<String, TreeMap<Integer, String>> hm to store keys. The value is TreeMap sorted based on timestamp.
set, update hm. get, first get the TreeMap based on key, then use floorKey to find the largest key <= timestamp.
Time Complexity: set, O(logn). get, O(logn). n = max(TreeMap size).
Space: O(m*n). m = hm.size().
AC Java:
class TimeMap {
HashMap<String, TreeMap<Integer, String>> hm;
/** Initialize your data structure here. */
public TimeMap() {
hm = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
hm.putIfAbsent(key, new TreeMap<>());
hm.get(key).put(timestamp, value);
}
public String get(String key, int timestamp) {
if(!hm.containsKey(key)){
return "";
}
TreeMap<Integer, String> item = hm.get(key);
Integer time = item.floorKey(timestamp);
return time == null ? "" : item.get(time);
}
}
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap obj = new TimeMap();
* obj.set(key,value,timestamp);
* String param_2 = obj.get(key,timestamp);
*/
LeetCode 981. Time Based Key-Value Store的更多相关文章
- Leetcode 981. Time Based Key-Value Store(二分查找)
题目来源:https://leetcode.com/problems/time-based-key-value-store/description/ 标记难度:Medium 提交次数:1/1 代码效率 ...
- etcd -> Highly-avaliable key value store for shared configuration and service discovery
The name "etcd" originated from two ideas, the unix "/etc" folder and "d&qu ...
- 【LeetCode】981. Time Based Key-Value Store 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【leetcode】981. Time Based Key-Value Store
题目如下: Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string ...
- LC 981. Time Based Key-Value Store
Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string key, s ...
- 【LeetCode】482. License Key Formatting 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode算法题-License Key Formatting(Java实现)
这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字 ...
- 【leetcode】482. License Key Formatting
problem 482. License Key Formatting solution1: 倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序. class Solution ...
- LeetCode 981.基于时间的键值存储(C++)
创建一个基于时间的键值存储类 TimeMap,它支持下面两个操作: 1. set(string key, string value, int timestamp) 存储键 key.值 value,以及 ...
随机推荐
- Sitecore 9 为什么数据驱动的组织选择它
Sitecore 9使用个性化和机器学习来帮助客户提高数字营销对数字投资的回报 Sitecore 9比以往任何时候都更加智能.主要功能包括: 数据集中化 向后兼容性 简单的迁移 该平台简化了营销人员和 ...
- 消除VS中动态申请二维数组C6011,C6385,C6386的警告
动态申请二维数组,无非就是通过指针来实现.@wowpH 过程分三步:1.申请内存,2.使用数组,3.释放内存. 代码如下: /************************************* ...
- groovy常用语法及实战
groovy语言简介 一种基于JVM的敏捷开发语言,作为编程语言可编译成java字节码,也可以作为脚本语言解释执行. 结合了Python.Ruby和Smalltalk的许多强大的特性 支持面向对象编程 ...
- Prometheus 监控linux服务器
Prometheus 监控linux服务器 node_exporter:用于*NIX系统监控,使用Go语言编写的收集器. 使用版本 node_exporter 0.17.0 相关文档 使用文档:htt ...
- Linux ssh 公私钥配置
Linux ssh 公私钥配置 ssh 公私钥可实现无密码的情况下直接直接登录到服务端.方便我们管理,而且也可以设置ssh完全通过公私钥登录,不可通过密码登录,来提高我们的服务器安全程度. 配置 生成 ...
- HDU校赛 | 2019 Multi-University Training Contest 3
2019 Multi-University Training Contest 3 http://acm.hdu.edu.cn/contests/contest_show.php?cid=850 100 ...
- 详细的Hadoop的入门教程-伪分布模式Pseudo-Distributed Operation
一. 伪分布模式Pseudo-Distributed Operation 这里关于VM虚拟机的安装就不再介绍了,详细请看<VMware虚拟机的三种网络管理模式>一章介绍.这章只介绍hado ...
- hibernate Criteria中多个or和and的用法 and ( or or)
/s筛选去除无效数据 /* detachedCriteria.add( Restrictions.or( Restrictions.like("chanpin", &qu ...
- Sping注解开发
基本注解 @Configuration 作用: 标记在类上表示是一个配置类(相当于一个配置类) @Bean 作用: 在容器中放一个bean相当于xml文件里的bean标签 @Configuration ...
- grub破解和bios加密
grub破解通过单用户模式,可以实现修改密码 grub加密以后,只能通过bios解除grub密码,方法如下 进入bios 修改启动方式,从CD启动 加载系统镜像,原系统默认挂载到/mnt/sysima ...