981:基于时间的键值存储

This commit is contained in:
huangge1199@hotmail.com 2021-07-12 21:38:47 +08:00
parent 6292e3eef8
commit a661873140
3 changed files with 165 additions and 1 deletions

View File

@ -0,0 +1,113 @@
//创建一个基于时间的键值存储类 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)
}

View File

@ -0,0 +1,51 @@
<p>创建一个基于时间的键值存储类&nbsp;<code>TimeMap</code>,它支持下面两个操作:</p>
<p>1. <code>set(string key, string value, int timestamp)</code></p>
<ul>
<li>存储键&nbsp;<code>key</code>、值&nbsp;<code>value</code>,以及给定的时间戳&nbsp;<code>timestamp</code></li>
</ul>
<p>2. <code>get(string key, int timestamp)</code></p>
<ul>
<li>返回先前调用&nbsp;<code>set(key, value, timestamp_prev)</code>&nbsp;所存储的值,其中&nbsp;<code>timestamp_prev &lt;= timestamp</code></li>
<li>如果有多个这样的值,则返回对应最大的&nbsp;&nbsp;<code>timestamp_prev</code>&nbsp;的那个值。</li>
<li>如果没有值,则返回空字符串(<code>&quot;&quot;</code>)。</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>inputs = [&quot;TimeMap&quot;,&quot;set&quot;,&quot;get&quot;,&quot;get&quot;,&quot;set&quot;,&quot;get&quot;,&quot;get&quot;], inputs = [[],[&quot;foo&quot;,&quot;bar&quot;,1],[&quot;foo&quot;,1],[&quot;foo&quot;,3],[&quot;foo&quot;,&quot;bar2&quot;,4],[&quot;foo&quot;,4],[&quot;foo&quot;,5]]
<strong>输出:</strong>[null,null,&quot;bar&quot;,&quot;bar&quot;,null,&quot;bar2&quot;,&quot;bar2&quot;]
<strong>解释:</strong>&nbsp;
TimeMap kv; &nbsp;
kv.set(&quot;foo&quot;, &quot;bar&quot;, 1); // 存储键 &quot;foo&quot; 和值 &quot;bar&quot; 以及时间戳 timestamp = 1 &nbsp;
kv.get(&quot;foo&quot;, 1); // 输出 &quot;bar&quot; &nbsp;
kv.get(&quot;foo&quot;, 3); // 输出 &quot;bar&quot; 因为在时间戳 3 和时间戳 2 处没有对应 &quot;foo&quot; 的值,所以唯一的值位于时间戳 1 处(即 &quot;bar&quot; &nbsp;
kv.set(&quot;foo&quot;, &quot;bar2&quot;, 4); &nbsp;
kv.get(&quot;foo&quot;, 4); // 输出 &quot;bar2&quot; &nbsp;
kv.get(&quot;foo&quot;, 5); // 输出 &quot;bar2&quot; &nbsp;
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>inputs = [&quot;TimeMap&quot;,&quot;set&quot;,&quot;set&quot;,&quot;get&quot;,&quot;get&quot;,&quot;get&quot;,&quot;get&quot;,&quot;get&quot;], inputs = [[],[&quot;love&quot;,&quot;high&quot;,10],[&quot;love&quot;,&quot;low&quot;,20],[&quot;love&quot;,5],[&quot;love&quot;,10],[&quot;love&quot;,15],[&quot;love&quot;,20],[&quot;love&quot;,25]]
<strong>输出:</strong>[null,null,null,&quot;&quot;,&quot;high&quot;,&quot;high&quot;,&quot;low&quot;,&quot;low&quot;]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li>所有的键/值字符串都是小写的。</li>
<li>所有的键/值字符串长度都在&nbsp;<code>[1, 100]</code>&nbsp;范围内。</li>
<li>所有&nbsp;<code>TimeMap.set</code>&nbsp;操作中的时间戳&nbsp;<code>timestamps</code> 都是严格递增的。</li>
<li><code>1 &lt;= timestamp &lt;= 10^7</code></li>
<li><code>TimeMap.set</code>&nbsp;<code>TimeMap.get</code>&nbsp;函数在每个测试用例中将(组合)调用总计&nbsp;<code>120000</code> 次。</li>
</ol>
<div><div>Related Topics</div><div><li>设计</li><li>哈希表</li><li>字符串</li><li>二分查找</li></div></div>\n<div><li>👍 120</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long