当前位置:网站首页>#981 Time Based Key-Value Store
#981 Time Based Key-Value Store
2022-06-12 20:59:00 【yoyooyooo】
Description
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key’s value at a certain timestamp.
Implement the TimeMap class:
- TimeMap() Initializes the object of the data structure.
- void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp.
- String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest timestamp_prev. If there are no values, it returns “”.
Examples
Example 1:
Input
[“TimeMap”, “set”, “get”, “get”, “set”, “get”, “get”]
[[], [“foo”, “bar”, 1], [“foo”, 1], [“foo”, 3], [“foo”, “bar2”, 4], [“foo”, 4], [“foo”, 5]]
Output
[null, null, “bar”, “bar”, null, “bar2”, “bar2”]
Explanation
TimeMap timeMap = new TimeMap();
timeMap.set(“foo”, “bar”, 1); // store the key “foo” and value “bar” along with timestamp = 1.
timeMap.get(“foo”, 1); // return “bar”
timeMap.get(“foo”, 3); // return “bar”, since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is “bar”.
timeMap.set(“foo”, “bar2”, 4); // store the key “foo” and value “bar2” along with timestamp = 4.
timeMap.get(“foo”, 4); // return “bar2”
timeMap.get(“foo”, 5); // return “bar2”
Constraints:
1 <= key.length, value.length <= 100
key and value consist of lowercase English letters and digits.
1 <= timestamp <= 1 0 7 10^7 107
All the timestamps timestamp of set are strictly increasing.
At most 2 * 105 calls will be made to set and get.
Ideas
Is to use a binary search idea , Because in constrain It says “All the timestamps timestamp of set are strictly increasing.”, So I set up an extra one directly list To store timestamp As the basis for the following binary search (set Is chaotic )
In addition, there is a precondition for binary search : To find no more than target One of the biggest timestamp, Therefore, the judgment of termination conditions is different from the ordinary binary search ( another , A non recursive solution is attached , By specifying a point as answer Assignment solves this problem )
Code
class TimeMap {
public Map<String, Map<Integer, String>> dict;
public Map<String, List<Integer>> stampMap;
public TimeMap() {
dict = new HashMap<>();
stampMap = new HashMap<>();
}
public int findNearestTimestamp(List<Integer> timestamps, int start, int end, int target) {
int middle = (start + end) / 2;
if (end <= start){
if (timestamps.get(end) > target)
return timestamps.get(end - 1);
return timestamps.get(end);
}
if (timestamps.get(middle) == target)
return target;
if (timestamps.get(middle) > target)
return findNearestTimestamp(timestamps, start, middle - 1, target);
else
return findNearestTimestamp(timestamps, middle + 1, end, target);
}
public void set(String key, String value, int timestamp) {
if (dict.containsKey(key)) {
Map<Integer, String> inside = dict.get(key);
inside.put(timestamp, value);
dict.put(key, inside);
List<Integer> stampInside = stampMap.get(key);
stampInside.add(timestamp);
stampMap.put(key, stampInside);
}
else {
Map<Integer, String> newInside = new HashMap<>();
newInside.put(timestamp, value);
dict.put(key, newInside);
List<Integer> stampInside = new ArrayList<>();
stampInside.add(timestamp);
stampMap.put(key, stampInside);
}
}
public String get(String key, int timestamp) {
if (!dict.containsKey(key))
return "";
List<Integer> timestamps = stampMap.get(key);
if (timestamp < timestamps.get(0))
return "";
int ts = findNearestTimestamp(timestamps, 0, timestamps.size() - 1, timestamp);
return dict.get(key).getOrDefault(ts, "");
}
}
Non recursive binary search
public int findNearestTimestamp(List<Integer> timestamps, int target) {
int start = 0;
int end = timestamps.size() - 1;
int answer = -1;
while(start <= end) {
int middle = (start + end) / 2;
if (timestamps.get(middle) <= target) {
answer = timestamps.get(middle);
start = middle + 1;
}
else
end = middle - 1;
}
return answer;
}
边栏推荐
- Gather function in pytorch_
- What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8
- Introduction to the characteristics of building a balancer decentralized exchange market capitalization robot
- 函数的了解
- Cv2.lut() (populates the output array with values from the lookup table)
- Algorinote_ 2_ Main theorem and Akra bazzi theorem
- Lua pattern matching
- Vs2017 environmental issues
- 新品发布丨竣达智能综合环境监测终端
- MySQL + PostgreSQL batch insert update insertorupdate
猜你喜欢

Nexus3搭建本地仓库

竣达技术丨适用于“科士达”智能精密空调网络监控

What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones

Why my order by create_ Time ASC becomes order by ASC

中小型机房动力环境综合监控解决方案

Zhangqiming, vice director of the United Front Work Department of the CPC Anhui Provincial Committee, led a team to investigate the HoloNet Royal Hefei R & D base

lintcode:127 · 拓扑排序

Data visualization - Calendar chart

入行5年从10k的功能测试到年薪40w的测试开发,花7天时间整理的超全学习路线

逐向双碳:东数西算中的绿色需求与竞争焦点
随机推荐
Solve the cvxpy error the solver GLPK_ MI is not installed
JS deep and shallow copy
Research Report on hydraulic injection machine industry - market status analysis and development prospect forecast
Introduction to scala basic grammar (III) various operators in Scala
Lintcode:127. Topology sorting
Properties to YML
JSP中的监听器
同花顺能开户吗,同花顺在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
Simplest ALV template
初步了解认识正则表达式(Regex)
金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
GPU giant NVIDIA suffered a "devastating" network attack, and the number one malware shut down its botnet infrastructure | global network security hotspot on February 28
Large and small end conversion
leetcode:210. Schedule II
How to improve communication efficiency during home office | community essay solicitation
最简单ALV模板
[leetcode 16 solution] the sum of the nearest three numbers
Nexus3 build local warehouse
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital