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 "";
}
};

最新文章

  1. PHP Date()函数详细参数
  2. Android获取系统时间方法的总结
  3. Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) E. Subordinates 贪心
  4. MySql中的事件
  5. linux系统启动级别
  6. 解决IE6下浮动层固定定位的经典方法
  7. mysql实例 保存查询结果到变量
  8. Android 签名(4)验证是否签名
  9. IgnoreRoute——注册路由
  10. SSL VPN 详解
  11. 谷歌 google
  12. python-摩尔斯电码查询器
  13. struts2.5新配置动态调用
  14. SAP进度条
  15. 检测flash是否安装及版本号
  16. 库函数strstr的实现
  17. shell1
  18. Qt QGraphicsItem要点 积累
  19. 机械臂运动学逆解(Analytical solution)
  20. jquery代码修改input的value值,而页面上input框的值没有改变的解决办法

热门文章

  1. 11 Windows编程——定时器
  2. 01 Windows编程——Hello World
  3. Vs2017 NetCode EF Mysql 控制台应用
  4. H5中的requestAnimationFrame
  5. 使用pyinstaller打包使用scrapy模块的程序运行时出现No such file or directory的问题解决
  6. 使用phpstudy搭建的外网网站 运行很慢 解决办法
  7. 四、vue基础--自定义组件
  8. PL/SQL老是自动断开问题处理
  9. asp.net用sql数据库生成json字符串并显示出来
  10. python通用分页功能