Skip to content

Commit 9c2b71e

Browse files
committed
feat: add solutions to leetcode problem: No.0981. Time Based Key-Value Store
1 parent d39ffd6 commit 9c2b71e

File tree

4 files changed

+163
-29
lines changed

4 files changed

+163
-29
lines changed

solution/0900-0999/0981.Time Based Key-Value Store/README.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,81 @@ kv.get("foo", 5); // 输出 "bar2"  
5757
<li><code>TimeMap.set</code> 和&nbsp;<code>TimeMap.get</code>&nbsp;函数在每个测试用例中将(组合)调用总计&nbsp;<code>120000</code> 次。</li>
5858
</ol>
5959

60-
6160
## 解法
6261

6362
<!-- 这里可写通用的实现逻辑 -->
6463

64+
嵌套哈希表实现。
65+
6566
<!-- tabs:start -->
6667

6768
### **Python3**
6869

6970
<!-- 这里可写当前语言的特殊实现逻辑 -->
7071

7172
```python
73+
class TimeMap:
74+
75+
def __init__(self):
76+
"""
77+
Initialize your data structure here.
78+
"""
79+
self.ktv = collections.defaultdict(list)
80+
81+
def set(self, key: str, value: str, timestamp: int) -> None:
82+
self.ktv[key].append((timestamp, value))
7283

84+
def get(self, key: str, timestamp: int) -> str:
85+
if key not in self.ktv:
86+
return ''
87+
tv = self.ktv[key]
88+
# #查找第一个大于timestamp的
89+
i = bisect.bisect_right(tv, (timestamp, chr(127)))
90+
return tv[i - 1][1] if i else ''
91+
92+
93+
94+
# Your TimeMap object will be instantiated and called as such:
95+
# obj = TimeMap()
96+
# obj.set(key,value,timestamp)
97+
# param_2 = obj.get(key,timestamp)
7398
```
7499

75100
### **Java**
76101

77102
<!-- 这里可写当前语言的特殊实现逻辑 -->
78103

79104
```java
80-
105+
class TimeMap {
106+
private Map<String, TreeMap<Integer, String>> ktv;
107+
108+
/** Initialize your data structure here. */
109+
public TimeMap() {
110+
ktv = new HashMap<>();
111+
}
112+
113+
public void set(String key, String value, int timestamp) {
114+
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
115+
tv.put(timestamp, value);
116+
ktv.put(key, tv);
117+
}
118+
119+
public String get(String key, int timestamp) {
120+
if (!ktv.containsKey(key)) {
121+
return "";
122+
}
123+
TreeMap<Integer, String> tv = ktv.get(key);
124+
Integer t = tv.floorKey(timestamp);
125+
return t == null ? "" : tv.get(t);
126+
}
127+
}
128+
129+
/**
130+
* Your TimeMap object will be instantiated and called as such:
131+
* TimeMap obj = new TimeMap();
132+
* obj.set(key,value,timestamp);
133+
* String param_2 = obj.get(key,timestamp);
134+
*/
81135
```
82136

83137
### **...**

solution/0900-0999/0981.Time Based Key-Value Store/README_EN.md

+53-27
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,26 @@
66

77
<p>Create a timebased key-value store class&nbsp;<code>TimeMap</code>, that supports two operations.</p>
88

9-
10-
119
<p>1. <code>set(string key, string value, int timestamp)</code></p>
1210

13-
14-
1511
<ul>
1612
<li>Stores the <code>key</code> and <code>value</code>, along with the given <code>timestamp</code>.</li>
1713
</ul>
1814

19-
20-
2115
<p>2. <code>get(string key, int timestamp)</code></p>
2216

23-
24-
2517
<ul>
2618
<li>Returns a value such that <code>set(key, value, timestamp_prev)</code> was called previously, with <code>timestamp_prev &lt;= timestamp</code>.</li>
2719
<li>If there are multiple such values, it returns the one with the largest <code>timestamp_prev</code>.</li>
2820
<li>If there are no values, it returns the empty string (<code>&quot;&quot;</code>).</li>
2921
</ul>
3022

31-
32-
3323
<p>&nbsp;</p>
3424

35-
36-
3725
<div>
3826

3927
<p><strong>Example 1:</strong></p>
4028

41-
42-
4329
<pre>
4430

4531
<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;
6652

6753
</pre>
6854

69-
70-
7155
<div>
7256

7357
<p><strong>Example 2:</strong></p>
7458

75-
76-
7759
<pre>
7860

7961
<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;
8668

8769
</div>
8870

89-
90-
9171
<p>&nbsp;</p>
9272

93-
94-
9573
<p><strong>Note:</strong></p>
9674

97-
98-
9975
<ol>
10076
<li>All key/value strings are lowercase.</li>
10177
<li>All key/value strings have&nbsp;length in the range&nbsp;<code>[1, 100]</code></li>
@@ -104,22 +80,72 @@ kv.get(&quot;foo&quot;, 5); //output &quot;bar2&quot; &nbsp;
10480
<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>
10581
</ol>
10682

107-
108-
10983
## Solutions
11084

11185
<!-- tabs:start -->
11286

11387
### **Python3**
11488

11589
```python
90+
class TimeMap:
91+
92+
def __init__(self):
93+
"""
94+
Initialize your data structure here.
95+
"""
96+
self.ktv = collections.defaultdict(list)
97+
98+
def set(self, key: str, value: str, timestamp: int) -> None:
99+
self.ktv[key].append((timestamp, value))
116100

101+
def get(self, key: str, timestamp: int) -> str:
102+
if key not in self.ktv:
103+
return ''
104+
tv = self.ktv[key]
105+
i = bisect.bisect_right(tv, (timestamp, chr(127)))
106+
return tv[i - 1][1] if i else ''
107+
108+
109+
110+
# Your TimeMap object will be instantiated and called as such:
111+
# obj = TimeMap()
112+
# obj.set(key,value,timestamp)
113+
# param_2 = obj.get(key,timestamp)
117114
```
118115

119116
### **Java**
120117

121118
```java
122-
119+
class TimeMap {
120+
private Map<String, TreeMap<Integer, String>> ktv;
121+
122+
/** Initialize your data structure here. */
123+
public TimeMap() {
124+
ktv = new HashMap<>();
125+
}
126+
127+
public void set(String key, String value, int timestamp) {
128+
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
129+
tv.put(timestamp, value);
130+
ktv.put(key, tv);
131+
}
132+
133+
public String get(String key, int timestamp) {
134+
if (!ktv.containsKey(key)) {
135+
return "";
136+
}
137+
TreeMap<Integer, String> tv = ktv.get(key);
138+
Integer t = tv.floorKey(timestamp);
139+
return t == null ? "" : tv.get(t);
140+
}
141+
}
142+
143+
/**
144+
* Your TimeMap object will be instantiated and called as such:
145+
* TimeMap obj = new TimeMap();
146+
* obj.set(key,value,timestamp);
147+
* String param_2 = obj.get(key,timestamp);
148+
*/
123149
```
124150

125151
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class TimeMap {
2+
private Map<String, TreeMap<Integer, String>> ktv;
3+
4+
/** Initialize your data structure here. */
5+
public TimeMap() {
6+
ktv = new HashMap<>();
7+
}
8+
9+
public void set(String key, String value, int timestamp) {
10+
TreeMap<Integer, String> tv = ktv.getOrDefault(key, new TreeMap<>());
11+
tv.put(timestamp, value);
12+
ktv.put(key, tv);
13+
}
14+
15+
public String get(String key, int timestamp) {
16+
if (!ktv.containsKey(key)) {
17+
return "";
18+
}
19+
TreeMap<Integer, String> tv = ktv.get(key);
20+
Integer t = tv.floorKey(timestamp);
21+
return t == null ? "" : tv.get(t);
22+
}
23+
}
24+
25+
/**
26+
* Your TimeMap object will be instantiated and called as such:
27+
* TimeMap obj = new TimeMap();
28+
* obj.set(key,value,timestamp);
29+
* String param_2 = obj.get(key,timestamp);
30+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class TimeMap:
2+
3+
def __init__(self):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.ktv = collections.defaultdict(list)
8+
9+
def set(self, key: str, value: str, timestamp: int) -> None:
10+
self.ktv[key].append((timestamp, value))
11+
12+
def get(self, key: str, timestamp: int) -> str:
13+
if key not in self.ktv:
14+
return ''
15+
tv = self.ktv[key]
16+
i = bisect.bisect_right(tv, (timestamp, chr(127)))
17+
return tv[i - 1][1] if i else ''
18+
19+
20+
21+
# Your TimeMap object will be instantiated and called as such:
22+
# obj = TimeMap()
23+
# obj.set(key,value,timestamp)
24+
# param_2 = obj.get(key,timestamp)

0 commit comments

Comments
 (0)