原题链接在这里: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. C++指针与数组、函数、动态内存分配

    C++指针 指针是用来存储地址的变量. 对于二维数组来说: a:代表的是首行地址: *a:代表的是首元素地址: **a:首元素: a+1:第二行地址: *a+2:首先*a是首元素地址,在首元素地址上+ ...

  2. Golang读取并修改非主流配置文件

    今天工作中碰到的问题,要求修改此配置文件,没看出来是什么格式,用了下面的思路: mysql { # If any of the files below are set, TLS encryption ...

  3. FusionInsight大数据开发---Streaming应用开发

    Streaming应用开发 掌握Streaming基本业务开发流 熟悉Streaming常用API接口使用 掌握Streaming业务设计基本原则 了解Streaming应用开发环境 了解CQL开发流 ...

  4. kafka controller脑裂(多个controller)问题

    问题:情况一:创建topic成功,但是produce的时候,却报unknown partition的错误,但zk上却显示了每个partition的leader信息:情况二: 给某个topic增加分区, ...

  5. SecureCRT上传本地文件到linux

    1.使用crt登录到需要操作的linux系统 2.按Alt+P打开sftp传输界面 3.输入pur指令加文件路径,例如:put E://srs-3.0.zip按enter就可以 4.再返回crt界面, ...

  6. 安装和使用pyltp

    什么是pyltp: pyltp 是LTP的 Python 封装,提供了分词,词性标注,命名实体识别,依存句法分析,语义角色标注的功能. 安装 pyltp 测试环境:系统win10 64位, pytho ...

  7. 网络编程之网络架构及其演变过程、互联网与互联网的组成、OSI七层协议、socket抽象层

    目录 网络架构及其演变过程 单机架构 CS架构 BS架构 BS架构和CS架构的区别 C/S架构的优缺点: B/S架构的优缺点: 互联网与互联网的组成 互联网的组成(教科书版) 互联网的组成(科普版) ...

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

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

  9. CDN: trunk Repo update failed - CocoaPods

    解决方案: 1.podfile文件中添加source源:  source 'https://github.com/CocoaPods/Specs.git' 2.执行 pod repo remove t ...

  10. 当前标识(IIS APPPOOL\DefaultAppPool)没有对“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”的写访问权限

    找到或增加这个目录,给他增加权限.