原题链接在这里: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 key and value, along with the given timestamp.

2. 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:

  1. All key/value strings are lowercase.
  2. All key/value strings have length in the range [1, 100]
  3. The timestamps for all TimeMap.set operations are strictly increasing.
  4. 1 <= timestamp <= 10^7
  5. TimeMap.set and TimeMap.get functions will be called a total of 120000 times (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的更多相关文章

  1. Leetcode 981. Time Based Key-Value Store(二分查找)

    题目来源:https://leetcode.com/problems/time-based-key-value-store/description/ 标记难度:Medium 提交次数:1/1 代码效率 ...

  2. 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 ...

  3. 【LeetCode】981. Time Based Key-Value Store 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  4. 【leetcode】981. Time Based Key-Value Store

    题目如下: Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string ...

  5. LC 981. Time Based Key-Value Store

    Create a timebased key-value store class TimeMap, that supports two operations. 1. set(string key, s ...

  6. 【LeetCode】482. License Key Formatting 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. LeetCode算法题-License Key Formatting(Java实现)

    这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字 ...

  8. 【leetcode】482. License Key Formatting

    problem 482. License Key Formatting solution1: 倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序. class Solution ...

  9. LeetCode 981.基于时间的键值存储(C++)

    创建一个基于时间的键值存储类 TimeMap,它支持下面两个操作: 1. set(string key, string value, int timestamp) 存储键 key.值 value,以及 ...

随机推荐

  1. Docker 制作定制asp.netcore 的容器

    上文Windows docker k8s asp.net core的k8swebap镜像只是一个asp.net core程序,在实际生产中我们希望容器中还有一些其他程序,比如ssh 和telegraf ...

  2. 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 ...

  3. TCP和UDP的差异

    TCP建立连接 如果有人问TCP如何建立连接?大部分的回答都是三次握手成功,就建立连接了. 那么握手的目的是什么呢? 1)告知对方自己的序号 2)初始化资源 例如以下握手案例,握手两次由客户端主动发起 ...

  4. OpenStack 中 RabbitMQ 的使用

    OpenStack 中 RabbitMQ 的使用 本文是 OpenStack 中的 RabbitMQ 使用研究 两部分中的第一部分,将介绍 RabbitMQ 的基本概念,即 RabbitMQ 是什么. ...

  5. U9-ERP BuildupDesigner 操作

    它的数据库文件配制     D:\UFIDA\UBFV50\U9.VOB.Product.UBF\UBFStudio\Runtime\environment.xml

  6. 看年薪50W的架构师如何手写一个SpringMVC框架

    前言 做 Java Web 开发的你,一定听说过SpringMVC的大名,作为现在运用最广泛的Java框架,它到目前为止依然保持着强大的活力和广泛的用户群. 本文介绍如何用eclipse一步一步搭建S ...

  7. 【转载】C#通过Contains方法判断DataTable中是否存在某个列名

    在C#中的Datatable数据变量的操作过程中,有时候需要判断DataTable中是否存在某个列名,此时可以通过DataTable对象的Columns列对象集合属性下的Contains方法来进行判断 ...

  8. CENTOS7-JAVA模拟CPU占用高及排查( 转)

    环境 centos7 1核2GB Java8 模拟cpu占用高 新建一个名为jvm-learn的springboot项目 模拟代码如下 import org.springframework.boot. ...

  9. linux系统crontab

    一.cron 简介 在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间. cro ...

  10. 12 ARM汇编

    Android系统采用java作为平台软件基础开发语言,NDK使Android平台可以运行C/C++代码这些代码汇编成ARM的elf可执行文件. 原生程序生成过程 经历4步:1.预处理2.编译3.汇编 ...