feat: add solutions to leetcode problem: No.0981. Time Based Key-Value Store

This commit is contained in:
yanglbme 2021-05-04 22:58:19 +08:00
parent d39ffd6349
commit 9c2b71e1e3
4 changed files with 161 additions and 27 deletions

View File

@ -57,11 +57,12 @@ kv.get("foo", 5); // 输出 "bar2"  
<li><code>TimeMap.set</code>&nbsp;<code>TimeMap.get</code>&nbsp;函数在每个测试用例中将(组合)调用总计&nbsp;<code>120000</code> 次。</li>
</ol>
## 解法
<!-- 这里可写通用的实现逻辑 -->
嵌套哈希表实现。
<!-- tabs:start -->
### **Python3**
@ -69,7 +70,31 @@ kv.get(&quot;foo&quot;, 5); // 输出 &quot;bar2&quot; &nbsp;
<!-- 这里可写当前语言的特殊实现逻辑 -->
```python
class TimeMap:
def __init__(self):
"""
Initialize your data structure here.
"""
self.ktv = collections.defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.ktv[key].append((timestamp, value))
def get(self, key: str, timestamp: int) -> str:
if key not in self.ktv:
return ''
tv = self.ktv[key]
# #查找第一个大于timestamp的
i = bisect.bisect_right(tv, (timestamp, chr(127)))
return tv[i - 1][1] if i else ''
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
```
### **Java**
@ -77,7 +102,36 @@ kv.get(&quot;foo&quot;, 5); // 输出 &quot;bar2&quot; &nbsp;
<!-- 这里可写当前语言的特殊实现逻辑 -->
```java
class TimeMap {
private Map<String, TreeMap<Integer, String>> ktv;
/** Initialize your data structure here. */
public TimeMap() {
ktv = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
tv.put(timestamp, value);
ktv.put(key, tv);
}
public String get(String key, int timestamp) {
if (!ktv.containsKey(key)) {
return "";
}
TreeMap<Integer, String> tv = ktv.get(key);
Integer t = tv.floorKey(timestamp);
return t == null ? "" : tv.get(t);
}
}
/**
* 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);
*/
```
### **...**

View File

@ -6,40 +6,26 @@
<p>Create a timebased key-value store class&nbsp;<code>TimeMap</code>, that supports two operations.</p>
<p>1. <code>set(string key, string value, int timestamp)</code></p>
<ul>
<li>Stores the <code>key</code> and <code>value</code>, along with the given <code>timestamp</code>.</li>
</ul>
<p>2. <code>get(string key, int timestamp)</code></p>
<ul>
<li>Returns a value such that <code>set(key, value, timestamp_prev)</code> was called previously, with <code>timestamp_prev &lt;= timestamp</code>.</li>
<li>If there are multiple such values, it returns the one with the largest <code>timestamp_prev</code>.</li>
<li>If there are no values, it returns the empty string (<code>&quot;&quot;</code>).</li>
</ul>
<p>&nbsp;</p>
<div>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>inputs = <span id="example-input-1-1">[&quot;TimeMap&quot;,&quot;set&quot;,&quot;get&quot;,&quot;get&quot;,&quot;set&quot;,&quot;get&quot;,&quot;get&quot;]</span>, inputs = <span id="example-input-1-2">[[],[&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]]</span>
@ -66,14 +52,10 @@ kv.get(&quot;foo&quot;, 5); //output &quot;bar2&quot; &nbsp;
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong>inputs = <span id="example-input-2-1">[&quot;TimeMap&quot;,&quot;set&quot;,&quot;set&quot;,&quot;get&quot;,&quot;get&quot;,&quot;get&quot;,&quot;get&quot;,&quot;get&quot;]</span>, inputs = <span id="example-input-2-2">[[],[&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]]</span>
@ -86,16 +68,10 @@ kv.get(&quot;foo&quot;, 5); //output &quot;bar2&quot; &nbsp;
</div>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ol>
<li>All key/value strings are lowercase.</li>
<li>All key/value strings have&nbsp;length in the range&nbsp;<code>[1, 100]</code></li>
@ -104,8 +80,6 @@ kv.get(&quot;foo&quot;, 5); //output &quot;bar2&quot; &nbsp;
<li><code>TimeMap.set</code> and <code>TimeMap.get</code>&nbsp;functions will be called a total of <code>120000</code> times (combined) per test case.</li>
</ol>
## Solutions
<!-- tabs:start -->
@ -113,13 +87,65 @@ kv.get(&quot;foo&quot;, 5); //output &quot;bar2&quot; &nbsp;
### **Python3**
```python
class TimeMap:
def __init__(self):
"""
Initialize your data structure here.
"""
self.ktv = collections.defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.ktv[key].append((timestamp, value))
def get(self, key: str, timestamp: int) -> str:
if key not in self.ktv:
return ''
tv = self.ktv[key]
i = bisect.bisect_right(tv, (timestamp, chr(127)))
return tv[i - 1][1] if i else ''
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
```
### **Java**
```java
class TimeMap {
private Map<String, TreeMap<Integer, String>> ktv;
/** Initialize your data structure here. */
public TimeMap() {
ktv = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
tv.put(timestamp, value);
ktv.put(key, tv);
}
public String get(String key, int timestamp) {
if (!ktv.containsKey(key)) {
return "";
}
TreeMap<Integer, String> tv = ktv.get(key);
Integer t = tv.floorKey(timestamp);
return t == null ? "" : tv.get(t);
}
}
/**
* 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);
*/
```
### **...**

View File

@ -0,0 +1,30 @@
class TimeMap {
private Map<String, TreeMap<Integer, String>> ktv;
/** Initialize your data structure here. */
public TimeMap() {
ktv = new HashMap<>();
}
public void set(String key, String value, int timestamp) {
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
tv.put(timestamp, value);
ktv.put(key, tv);
}
public String get(String key, int timestamp) {
if (!ktv.containsKey(key)) {
return "";
}
TreeMap<Integer, String> tv = ktv.get(key);
Integer t = tv.floorKey(timestamp);
return t == null ? "" : tv.get(t);
}
}
/**
* 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);
*/

View File

@ -0,0 +1,24 @@
class TimeMap:
def __init__(self):
"""
Initialize your data structure here.
"""
self.ktv = collections.defaultdict(list)
def set(self, key: str, value: str, timestamp: int) -> None:
self.ktv[key].append((timestamp, value))
def get(self, key: str, timestamp: int) -> str:
if key not in self.ktv:
return ''
tv = self.ktv[key]
i = bisect.bisect_right(tv, (timestamp, chr(127)))
return tv[i - 1][1] if i else ''
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)