原题链接在这里: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. maven 引入qrcode.jar

        mvn install:install-file -Dfile=e:\QRCode.jar -DgroupId=QRCode -DartifactId=QRCode -Dversion=3.0 ...

  2. vs2019发布Web到云服务器(IIS)

    捣鼓了也有几天,到处收集资料终于折腾出来,做点小笔记 原文地址:https://www.cnblogs.com/potential/p/3751426.html 一.我的环境: Windows Ser ...

  3. Windows下非PE方式载荷投递方式研究

    0. 引言 0x1:载荷是什么?在整个入侵过程中起到什么作用? 载荷的作用在整个入侵链路的作用起到纽带的作用,它借助于目标系统提供的某些功能:组件:执行环境,将攻击者的传递的恶意payload包裹起来 ...

  4. 通过静态发现方式部署 Etcd 集群

    在「etcd使用入门」一文中对etcd的基本知识点和安装做了一个简要的介绍,这次我们来说说如何部署一个etcd集群. etcd构建自身高可用集群主要有三种形式: 静态发现: 预先已知etcd集群中有哪 ...

  5. 论文笔记:LightGBM: A Highly Efficient Gradient Boosting Decision Tree

    引言 GBDT已经有了比较成熟的应用,例如XGBoost和pGBRT,但是在特征维度很高数据量很大的时候依然不够快.一个主要的原因是,对于每个特征,他们都需要遍历每一条数据,对每一个可能的分割点去计算 ...

  6. phoenix kerberos 连接配置

    1. 官网资料 Use JDBC to get a connection to an HBase cluster like this: Connection conn = DriverManager. ...

  7. 指针总结指向const的指针、const指针、指向const指针的const指针

    指针的一些总结   const与指针 指向const的指针指的是指针指向的数据是常量,不可以被修改,但指针变量本身可以被修改,如const int *p:严格说不能用指针间接修改指向的数据,但该变量可 ...

  8. BZOJ1040: [ZJOI2008]骑士(奇环树,DP)

    题目: 1040: [ZJOI2008]骑士 解析: 假设骑士\(u\)讨厌骑士\(v\),我们在\(u\),\(v\)之间连一条边,这样我们就得到了一个奇环树(奇环森林),既然是一颗奇环树,我们就先 ...

  9. Java之路---Day19(set接口)

    set接口 java.util.Set 接口和 java.util.List 接口一样,同样继承自 Collection 接口,它与 Collection 接口中的方 法基本一致,但是set接口中元素 ...

  10. Java自学-面向对象 属性

    Java类的属性 一个英雄有姓名,血量,护甲等等状态 这些状态就叫做一个类的属性 步骤 1 : 属性的类型 属性的类型可以是基本类型,比如int整数,float 浮点数 也可以是类类型,比如Strin ...