当前位置:网站首页>#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;
}
边栏推荐
- Transaction code qs28 of SAP QM preliminary level
- Scope and scope chain
- Restful API 接口规范
- HR SaaS unicorn is about to emerge. Will the employee experience be the next explosive point?
- UVa11991 Easy Problem from Rujia Liu
- 金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试
- Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
- 做自媒体视频,友好的新媒体运营必备app分享
- Why my order by create_ Time ASC becomes order by ASC
- Market trend report, technical innovation and market forecast of hydraulic torque wrench in China
猜你喜欢

Listener in JSP

字符串基础知识

测试人如何规划自己的未来?才能实现入行2年达到25k?

Lightroom Ambassador series: capturing nostalgia with MEG loeks

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

The required books for software testers (with e-books) recommended by senior Ali have benefited me a lot

Introduction to the characteristics of building a balancer decentralized exchange market capitalization robot

View 的事件分发机制

How do testers plan for their future? To achieve 25K in 2 years?

Design rule check constraint (set_max_transition, set_max_capability)
随机推荐
remote: Support for password authentication was removed on August 13, 2021
Preliminary understanding of regular expressions (regex)
Properties to YML
机器学习资料汇总
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
Nexus3搭建本地仓库
Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?
China hydraulic press market trend report, technical innovation and market forecast
Mxnet record IO details
Event distribution mechanism of view
new做了哪几件事
Social metauniverse: start from redefining yourself
Restful API interface specification
leetcode:207. Class Schedule Card
[leetcode 16 solution] the sum of the nearest three numbers
Leetcode: 210. Programme II
Unauthorized rce in VMware vCenter
竣达技术丨适用于“科士达”智能精密空调网络监控
What did new do
At the beginning of SAP QM, assign sampling strategy for quantitative characteristics