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 ("").
Runtime: 212 ms, faster than 55.01% of C++ online submissions for Time Based Key-Value Store.
Memory Usage: 57 MB, less than 100.00% of C++ online submissions for Time Based Key-Value Store.
class TimeMap {
private:
unordered_map<string, map<int, string>> mp;
vector<int> tvec;
public:
/** Initialize your data structure here. */
TimeMap() {} void set(string key, string value, int timestamp) {
mp[key][timestamp] = value;
} string get(string key, int timestamp) {
if(!mp.count(key)) return "";
if(mp[key].count(timestamp)) return mp[key][timestamp];
for(auto it = mp[key].rbegin(); it != mp[key].rend(); it++) {
if(it->first > timestamp) continue;
else {
return it->second;
}
}
return "";
}
};

LC 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

    原题链接在这里:https://leetcode.com/problems/time-based-key-value-store/ 题目: Create a timebased key-value s ...

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

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

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

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

  6. 一些开源搜索引擎实现——倒排使用原始文件,列存储Hbase,KV store如levelDB、mongoDB、redis,以及SQL的,如sqlite或者xxSQL

    本文说明:除开ES,Solr,sphinx系列的其他开源搜索引擎汇总于此.   A search engine based on Node.js and LevelDB A persistent, n ...

  7. Claims Based Authentication and Token Based Authentication和WIF

    基于声明的认证方式,其最大特性是可传递(一方面是由授信的Issuer,即claims持有方,发送到你的应用上,注意信任是单向的.例如QQ集成登录,登录成功后,QQ会向你的应用发送claims.另一方面 ...

  8. External Configuration Store Pattern 外部配置存储模式

    Move configuration information out of the application deployment package to a centralized location. ...

  9. Redis Installation、Configuration、Program Based On Redis Learning

    目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...

随机推荐

  1. python网络爬虫第三弹(<爬取get请求的页面数据>)

    一.urllib库 urllib是python自带的一个用于爬虫的库,其主要作用就是通过代码模拟浏览器发送请求,其常被用到的子模块在 python3中的为urllib.request 和 urllib ...

  2. synchronized 和 ReentrantLock 区别是什么?(未完成)

    synchronized 和 ReentrantLock 区别是什么?(未完成)

  3. ArrayList 和 LinkedList 的区别是什么?(未完成)

    ArrayList 和 LinkedList 的区别是什么?(未完成)

  4. js-虚拟dom

    问题: vdom是什么?为什么存在vdom? vdom是如何应用的,核心的api是什么? 介绍一下diff算法 1.一些虚拟dom应用了snabbdom.其中的 h函数相当于渲染成了右侧的JS虚拟节点 ...

  5. easyui-filebox上传图片到阿里

    应用场景:在fixbox图片上传时在预览图片img标签底下点击按钮触发一下函数 参考:https://www.cnblogs.com/awzf/p/9843814.html js //修改该时上传产品 ...

  6. ACM-ICPC 2018 徐州赛区现场赛 I. Rikka with Sorting Networks (思维+DFS)

    题目链接:https://codeforces.com/gym/102012/problem/I 题意:问有多少个 1 到 n 的排列,使得用给定的 k 个比较器(使 au 和 av 有序)排序后,整 ...

  7. MySQL Data Directory -- Creating file-per-table tablespaces outside the data directory

    Creating file-per-table tablespaces outside the data directory 一. Data Directory 1.应对情况 当数据库所在空间不足的时 ...

  8. oracle自连接

    自连接:通过表的别名,将同一张表视为多张表 select e.ename 员工姓名,b.ename 老板姓名 from emp e,emp b where e.mgr=b.empno; 注:自连接不适 ...

  9. 数据库学习之二--SQL语句以及数据类型

    一.SQL语句种类: 1. DDL(Data Definition Language,数据定义语言)用来创建或者删除存储数据用的数据库以及数据库中的表;包含以下几种指令: a. CREATE:CREA ...

  10. mfc编程之发送wm_paint消息时绘图界面只出现一瞬间

    实现的功能是在打开一个文件然后把文件的图形信息绘制在picture控件上. 问题描述:我把绘制的操作放在了窗口中onpaint()函数里,打开文件后发送一个wm_paint的消息,发现picture控 ...