mirror of https://github.com/doocs/leetcode.git
feat: add solutions to leetcode problem: No.0981. Time Based Key-Value Store
This commit is contained in:
parent
d39ffd6349
commit
9c2b71e1e3
|
|
@ -57,11 +57,12 @@ kv.get("foo", 5); // 输出 "bar2"
|
|||
<li><code>TimeMap.set</code> 和 <code>TimeMap.get</code> 函数在每个测试用例中将(组合)调用总计 <code>120000</code> 次。</li>
|
||||
</ol>
|
||||
|
||||
|
||||
## 解法
|
||||
|
||||
<!-- 这里可写通用的实现逻辑 -->
|
||||
|
||||
嵌套哈希表实现。
|
||||
|
||||
<!-- tabs:start -->
|
||||
|
||||
### **Python3**
|
||||
|
|
@ -69,7 +70,31 @@ kv.get("foo", 5); // 输出 "bar2"
|
|||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```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("foo", 5); // 输出 "bar2"
|
|||
<!-- 这里可写当前语言的特殊实现逻辑 -->
|
||||
|
||||
```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);
|
||||
*/
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
|
|
|||
|
|
@ -6,40 +6,26 @@
|
|||
|
||||
<p>Create a timebased key-value store class <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 <= 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>""</code>).</li>
|
||||
</ul>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
|
||||
|
||||
<div>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input: </strong>inputs = <span id="example-input-1-1">["TimeMap","set","get","get","set","get","get"]</span>, inputs = <span id="example-input-1-2">[[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]</span>
|
||||
|
|
@ -66,14 +52,10 @@ kv.get("foo", 5); //output "bar2"
|
|||
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
<div>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input: </strong>inputs = <span id="example-input-2-1">["TimeMap","set","set","get","get","get","get","get"]</span>, inputs = <span id="example-input-2-2">[[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]]</span>
|
||||
|
|
@ -86,16 +68,10 @@ kv.get("foo", 5); //output "bar2"
|
|||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
|
||||
|
||||
<ol>
|
||||
<li>All key/value strings are lowercase.</li>
|
||||
<li>All key/value strings have length in the range <code>[1, 100]</code></li>
|
||||
|
|
@ -104,8 +80,6 @@ kv.get("foo", 5); //output "bar2"
|
|||
<li><code>TimeMap.set</code> and <code>TimeMap.get</code> 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("foo", 5); //output "bar2"
|
|||
### **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);
|
||||
*/
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
*/
|
||||
|
|
@ -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)
|
||||
Loading…
Reference in New Issue