Skip to content

Commit

Permalink
Add solution and test-cases for problem 981
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Aug 21, 2024
1 parent 9f83439 commit 73af4cd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 26 deletions.
38 changes: 21 additions & 17 deletions leetcode/901-1000/0981.Time-Based-Key-Value-Store/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
# [981.Time Based Key-Value Store][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Implement the `TimeMap` class:

## 题意
> ...
- `TimeMap()` Initializes the object of the data structure.
- `void set(String key, String value, int timestamp)` Stores the `key` key with the `value` value at the given time `timestamp`.
- `String get(String key, int timestamp)` Returns a value such that `set` was called previously, with `timestamp_prev <= timestamp`. If there are multiple such values, it returns the value associated with the largest `timestamp_prev`. If there are no values, it returns `""`.

## 题解
**Example 1:**

### 思路1
> ...
Time Based Key-Value Store
```go
```

Input
["TimeMap", "set", "get", "get", "set", "get", "get"]
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
Output
[null, null, "bar", "bar", null, "bar2", "bar2"]
Explanation
TimeMap timeMap = new TimeMap();
timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1.
timeMap.get("foo", 1); // return "bar"
timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar".
timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4.
timeMap.get("foo", 4); // return "bar2"
timeMap.get("foo", 5); // return "bar2"
```

## 结语

Expand Down
55 changes: 53 additions & 2 deletions leetcode/901-1000/0981.Time-Based-Key-Value-Store/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

type storeItem struct {
timestamp int
val string
}
type TimeMap struct {
data map[string][]storeItem
}

func Constructor981() TimeMap {
return TimeMap{
data: make(map[string][]storeItem),
}
}

func (this *TimeMap) Set(key string, value string, timestamp int) {
if _, ok := this.data[key]; !ok {
this.data[key] = make([]storeItem, 0)
}
this.data[key] = append(this.data[key], storeItem{timestamp, value})
}

func (this *TimeMap) Get(key string, timestamp int) string {
v, ok := this.data[key]
if !ok {
return ""
}
idx := sort.Search(len(v), func(i int) bool {
return v[i].timestamp > timestamp
})
if idx == 0 {
return ""
}
return v[idx-1].val
}

type input struct {
key, value string
timestamp int
}

func Solution(inputs []input) []string {
c := Constructor981()
ans := make([]string, 0)
for _, i := range inputs {
if i.value == "" {
ans = append(ans, c.Get(i.key, i.timestamp))
continue
}
c.Set(i.key, i.value, i.timestamp)
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []input
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []input{{"foo", "bar", 1}, {"foo", "", 1}, {"foo", "", 3}, {"foo", "bar2", 4}, {"foo", "", 4}, {"foo", "", 5}}, []string{"bar", "bar", "bar2", "bar2"}},
}

// 开始测试
Expand All @@ -30,10 +28,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 73af4cd

Please sign in to comment.