Skip to content

Commit fb387a2

Browse files
committed
添加 problem 981
1 parent 6542d73 commit fb387a2

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
type data struct {
6+
time int
7+
value string
8+
}
9+
10+
// TimeMap is a timebased key-value store
11+
// TimeMap define
12+
type TimeMap map[string][]data
13+
14+
// Constructor981 define
15+
func Constructor981() TimeMap {
16+
return make(map[string][]data, 1024)
17+
}
18+
19+
// Set define
20+
func (t TimeMap) Set(key string, value string, timestamp int) {
21+
if _, ok := t[key]; !ok {
22+
t[key] = make([]data, 1, 1024)
23+
}
24+
t[key] = append(t[key], data{
25+
time: timestamp,
26+
value: value,
27+
})
28+
}
29+
30+
// Get define
31+
func (t TimeMap) Get(key string, timestamp int) string {
32+
d := t[key]
33+
i := sort.Search(len(d), func(i int) bool {
34+
return timestamp < d[i].time
35+
})
36+
i--
37+
return t[key][i].value
38+
}
39+
40+
/**
41+
* Your TimeMap object will be instantiated and called as such:
42+
* obj := Constructor();
43+
* obj.Set(key,value,timestamp);
44+
* param_2 := obj.Get(key,timestamp);
45+
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func Test_Problem981(t *testing.T) {
9+
obj := Constructor981()
10+
obj.Set("foo", "bar", 1)
11+
fmt.Printf("Get = %v\n", obj.Get("foo", 1))
12+
fmt.Printf("Get = %v\n", obj.Get("foo", 3))
13+
obj.Set("foo", "bar2", 4)
14+
fmt.Printf("Get = %v\n", obj.Get("foo", 4))
15+
fmt.Printf("Get = %v\n", obj.Get("foo", 5))
16+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [981. Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/)
2+
3+
4+
## 题目:
5+
6+
Create a timebased key-value store class `TimeMap`, that supports two operations.
7+
8+
1. `set(string key, string value, int timestamp)`
9+
10+
- Stores the `key` and `value`, along with the given `timestamp`.
11+
12+
2. `get(string key, int timestamp)`
13+
14+
- Returns a value such that `set(key, value, timestamp_prev)` was called previously, with `timestamp_prev <= timestamp`.
15+
- If there are multiple such values, it returns the one with the largest `timestamp_prev`.
16+
- If there are no values, it returns the empty string (`""`).
17+
18+
**Example 1:**
19+
20+
Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
21+
Output: [null,null,"bar","bar",null,"bar2","bar2"]
22+
Explanation:
23+
TimeMap kv;
24+
kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1
25+
kv.get("foo", 1); // output "bar"
26+
kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"
27+
kv.set("foo", "bar2", 4);
28+
kv.get("foo", 4); // output "bar2"
29+
kv.get("foo", 5); //output "bar2"
30+
31+
**Example 2:**
32+
33+
Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]]
34+
Output: [null,null,null,"","high","high","low","low"]
35+
36+
**Note:**
37+
38+
1. All key/value strings are lowercase.
39+
2. All key/value strings have length in the range `[1, 100]`
40+
3. The `timestamps` for all `TimeMap.set` operations are strictly increasing.
41+
4. `1 <= timestamp <= 10^7`
42+
5. `TimeMap.set` and `TimeMap.get` functions will be called a total of `120000` times (combined) per test case.
43+
44+
## 题目大意
45+
46+
创建一个基于时间的键值存储类 TimeMap,它支持下面两个操作:
47+
48+
1. set(string key, string value, int timestamp)
49+
50+
- 存储键 key、值 value,以及给定的时间戳 timestamp。
51+
52+
2. get(string key, int timestamp)
53+
54+
- 返回先前调用 set(key, value, timestamp_prev) 所存储的值,其中 timestamp_prev <= timestamp。
55+
- 如果有多个这样的值,则返回对应最大的  timestamp_prev 的那个值。
56+
- 如果没有值,则返回空字符串("")。
57+
58+
提示:
59+
60+
1. 所有的键/值字符串都是小写的。
61+
2. 所有的键/值字符串长度都在 [1, 100] 范围内。
62+
3. 所有 TimeMap.set 操作中的时间戳 timestamps 都是严格递增的。
63+
4. 1 <= timestamp <= 10^7
64+
5. TimeMap.set 和 TimeMap.get 函数在每个测试用例中将(组合)调用总计 120000 次。
65+
66+
67+
## 解题思路
68+
69+
- 要求设计一个基于时间戳的 `kv` 存储。`set()` 操作里面会会包含一个时间戳。get() 操作的时候查找时间戳小于等于 `timestamp``key` 对应的 `value`,如果有多个解,输出满足条件的最大时间戳对应的 `value` 值。
70+
- 这一题可以用二分搜索来解答,用 `map` 存储 `kv` 数据,`key` 对应的就是 `key``value` 对应一个结构体,里面包含 `value``timestamp`。执行 `get()` 操作的时候,先取出 `key` 对应的结构体数组,然后在这个数组里面根据 `timestamp` 进行二分搜索。由于题意是要找小于等于 `timestamp` 的最大 `timestamp` ,这会有很多满足条件的解,变换一下,先找 `> timestamp` 的最小解,然后下标减一即是满足题意的最大解。
71+
- 另外题目中提到“`TimeMap.set` 操作中的 `timestamp` 是严格递增的”。所以在 `map` 中存储 `value` 结构体的时候,不需要排序了,天然有序。

0 commit comments

Comments
 (0)