leet-code/src/main/java/leetcode/editor/cn/TimeBasedKeyValueStore.java
2021-07-12 21:38:47 +08:00

113 lines
3.5 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//创建一个基于时间的键值存储类 TimeMap它支持下面两个操作
//
// 1. set(string key, string value, int timestamp)
//
//
// 存储键 key、值 value以及给定的时间戳 timestamp。
//
//
// 2. get(string key, int timestamp)
//
//
// 返回先前调用 set(key, value, timestamp_prev) 所存储的值,其中 timestamp_prev <= timestamp。
//
// 如果有多个这样的值,则返回对应最大的 timestamp_prev 的那个值。
// 如果没有值,则返回空字符串("")。
//
//
//
//
// 示例 1
//
// 输入inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["f
//oo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
//输出:[null,null,"bar","bar",null,"bar2","bar2"]
//解释: 
//TimeMap kv;  
//kv.set("foo", "bar", 1); // 存储键 "foo" 和值 "bar" 以及时间戳 timestamp = 1  
//kv.get("foo", 1); // 输出 "bar"  
//kv.get("foo", 3); // 输出 "bar" 因为在时间戳 3 和时间戳 2 处没有对应 "foo" 的值,所以唯一的值位于时间戳 1 处(即
// "bar"  
//kv.set("foo", "bar2", 4);  
//kv.get("foo", 4); // 输出 "bar2"  
//kv.get("foo", 5); // 输出 "bar2"  
//
//
//
// 示例 2
//
// 输入inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [
//[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["lov
//e",20],["love",25]]
//输出:[null,null,null,"","high","high","low","low"]
//
//
//
//
// 提示:
//
//
// 所有的键/值字符串都是小写的。
// 所有的键/值字符串长度都在 [1, 100] 范围内。
// 所有 TimeMap.set 操作中的时间戳 timestamps 都是严格递增的。
// 1 <= timestamp <= 10^7
// TimeMap.set 和 TimeMap.get 函数在每个测试用例中将(组合)调用总计 120000 次。
//
// Related Topics 设计 哈希表 字符串 二分查找
// 👍 120 👎 0
package leetcode.editor.cn;
import java.util.*;
//981:基于时间的键值存储
class TimeBasedKeyValueStore {
public static void main(String[] args) {
//测试代码
// Solution solution = new TimeBasedKeyValueStore().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class TimeMap {
class Node {
String k, v;
int t;
Node (String _k, String _v, int _t) {
k = _k; v = _v; t = _t;
}
}
Map<String, List<Node>> map = new HashMap<>();
public void set(String k, String v, int t) {
List<Node> list = map.getOrDefault(k, new ArrayList<>());
list.add(new Node(k, v, t));
map.put(k, list);
}
public String get(String k, int t) {
List<Node> list = map.getOrDefault(k, new ArrayList<>());
if (list.isEmpty()) return "";
int n = list.size();
int l = 0, r = n - 1;
while (l < r) {
int mid = l + r + 1 >> 1;
if (list.get(mid).t <= t) {
l = mid;
} else {
r = mid - 1;
}
}
return list.get(r).t <= t ? list.get(r).v : "";
}
}
/**
* 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 submit region end(Prohibit modification and deletion)
}