Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 968ccbb

Browse files
feat: add golang solution to lc problem: No. 0981.Time Based Key-Value Store
1 parent 535ff81 commit 968ccbb

File tree

3 files changed

+114
-16
lines changed

3 files changed

+114
-16
lines changed

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828

2929
<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]]
3030
<strong>输出:</strong>[null,null,&quot;bar&quot;,&quot;bar&quot;,null,&quot;bar2&quot;,&quot;bar2&quot;]
31-
<strong>解释:</strong>&nbsp;
32-
TimeMap kv; &nbsp;
33-
kv.set(&quot;foo&quot;, &quot;bar&quot;, 1); // 存储键 &quot;foo&quot; 和值 &quot;bar&quot; 以及时间戳 timestamp = 1 &nbsp;
34-
kv.get(&quot;foo&quot;, 1); // 输出 &quot;bar&quot; &nbsp;
35-
kv.get(&quot;foo&quot;, 3); // 输出 &quot;bar&quot; 因为在时间戳 3 和时间戳 2 处没有对应 &quot;foo&quot; 的值,所以唯一的值位于时间戳 1 处(即 &quot;bar&quot;) &nbsp;
36-
kv.set(&quot;foo&quot;, &quot;bar2&quot;, 4); &nbsp;
37-
kv.get(&quot;foo&quot;, 4); // 输出 &quot;bar2&quot; &nbsp;
38-
kv.get(&quot;foo&quot;, 5); // 输出 &quot;bar2&quot; &nbsp;
31+
<strong>解释:</strong>&nbsp;
32+
TimeMap kv; &nbsp;
33+
kv.set(&quot;foo&quot;, &quot;bar&quot;, 1); // 存储键 &quot;foo&quot; 和值 &quot;bar&quot; 以及时间戳 timestamp = 1 &nbsp;
34+
kv.get(&quot;foo&quot;, 1); // 输出 &quot;bar&quot; &nbsp;
35+
kv.get(&quot;foo&quot;, 3); // 输出 &quot;bar&quot; 因为在时间戳 3 和时间戳 2 处没有对应 &quot;foo&quot; 的值,所以唯一的值位于时间戳 1 处(即 &quot;bar&quot;) &nbsp;
36+
kv.set(&quot;foo&quot;, &quot;bar2&quot;, 4); &nbsp;
37+
kv.get(&quot;foo&quot;, 4); // 输出 &quot;bar2&quot; &nbsp;
38+
kv.get(&quot;foo&quot;, 5); // 输出 &quot;bar2&quot; &nbsp;
3939

4040
</pre>
4141

@@ -134,6 +134,41 @@ class TimeMap {
134134
*/
135135
```
136136

137+
### **Go**
138+
139+
因为 timestamp 是一直增长的,所以可以用二分查找快速找到值
140+
141+
```go
142+
type pair struct {
143+
timestamp int
144+
value string
145+
}
146+
147+
type TimeMap struct {
148+
data map[string][]pair
149+
}
150+
151+
func Constructor() TimeMap {
152+
return TimeMap{data: make(map[string][]pair)}
153+
}
154+
155+
func (m *TimeMap) Set(key string, value string, timestamp int) {
156+
m.data[key] = append(m.data[key], pair{timestamp, value})
157+
}
158+
159+
func (m *TimeMap) Get(key string, timestamp int) string {
160+
pairs := m.data[key]
161+
// sort.Search return the smallest index i in [0, n) at which f(i) is true
162+
i := sort.Search(len(pairs), func(i int) bool {
163+
return pairs[i].timestamp > timestamp
164+
})
165+
if i > 0 {
166+
return pairs[i-1].value
167+
}
168+
return ""
169+
}
170+
```
171+
137172
### **...**
138173

139174
```

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@
3232

3333
<strong>Output: </strong><span id="example-output-1">[null,null,&quot;bar&quot;,&quot;bar&quot;,null,&quot;bar2&quot;,&quot;bar2&quot;]</span>
3434

35-
<strong>Explanation: </strong><span id="example-output-1">&nbsp;
35+
<strong>Explanation: </strong><span id="example-output-1">&nbsp;
3636

37-
TimeMap kv; &nbsp;
37+
TimeMap kv; &nbsp;
3838

39-
kv.set(&quot;foo&quot;, &quot;bar&quot;, 1); // store the key &quot;foo&quot; and value &quot;bar&quot; along with timestamp = 1 &nbsp;
39+
kv.set(&quot;foo&quot;, &quot;bar&quot;, 1); // store the key &quot;foo&quot; and value &quot;bar&quot; along with timestamp = 1 &nbsp;
4040

41-
kv.get(&quot;foo&quot;, 1); // output &quot;bar&quot; &nbsp;
41+
kv.get(&quot;foo&quot;, 1); // output &quot;bar&quot; &nbsp;
4242

43-
kv.get(&quot;foo&quot;, 3); // output &quot;bar&quot; since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie &quot;bar&quot; &nbsp;
43+
kv.get(&quot;foo&quot;, 3); // output &quot;bar&quot; since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie &quot;bar&quot; &nbsp;
4444

45-
kv.set(&quot;foo&quot;, &quot;bar2&quot;, 4); &nbsp;
45+
kv.set(&quot;foo&quot;, &quot;bar2&quot;, 4); &nbsp;
4646

47-
kv.get(&quot;foo&quot;, 4); // output &quot;bar2&quot; &nbsp;
47+
kv.get(&quot;foo&quot;, 4); // output &quot;bar2&quot; &nbsp;
4848

49-
kv.get(&quot;foo&quot;, 5); //output &quot;bar2&quot; &nbsp;
49+
kv.get(&quot;foo&quot;, 5); //output &quot;bar2&quot; &nbsp;
5050

5151
</span>
5252

@@ -148,6 +148,41 @@ class TimeMap {
148148
*/
149149
```
150150

151+
### **Go**
152+
153+
Because timestamp is always increasing, you can use binary search to quickly find the value
154+
155+
```go
156+
type pair struct {
157+
timestamp int
158+
value string
159+
}
160+
161+
type TimeMap struct {
162+
data map[string][]pair
163+
}
164+
165+
func Constructor() TimeMap {
166+
return TimeMap{data: make(map[string][]pair)}
167+
}
168+
169+
func (m *TimeMap) Set(key string, value string, timestamp int) {
170+
m.data[key] = append(m.data[key], pair{timestamp, value})
171+
}
172+
173+
func (m *TimeMap) Get(key string, timestamp int) string {
174+
pairs := m.data[key]
175+
// sort.Search return the smallest index i in [0, n) at which f(i) is true
176+
i := sort.Search(len(pairs), func(i int) bool {
177+
return pairs[i].timestamp > timestamp
178+
})
179+
if i > 0 {
180+
return pairs[i-1].value
181+
}
182+
return ""
183+
}
184+
```
185+
151186
### **...**
152187

153188
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
type pair struct {
2+
timestamp int
3+
value string
4+
}
5+
6+
type TimeMap struct {
7+
data map[string][]pair
8+
}
9+
10+
func Constructor() TimeMap {
11+
return TimeMap{data: make(map[string][]pair)}
12+
}
13+
14+
func (m *TimeMap) Set(key string, value string, timestamp int) {
15+
m.data[key] = append(m.data[key], pair{timestamp, value})
16+
}
17+
18+
func (m *TimeMap) Get(key string, timestamp int) string {
19+
pairs := m.data[key]
20+
// sort.Search return the smallest index i in [0, n) at which f(i) is true
21+
i := sort.Search(len(pairs), func(i int) bool {
22+
return pairs[i].timestamp > timestamp
23+
})
24+
if i > 0 {
25+
return pairs[i-1].value
26+
}
27+
return ""
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /