Skip to content

Commit 937db82

Browse files
authored
Merge pull request #1116 from 0xff-dev/2349
Add solution and test-cases for problem 2349
2 parents b21e47d + ee85c6b commit 937db82

File tree

3 files changed

+123
-25
lines changed

3 files changed

+123
-25
lines changed

leetcode/2301-2400/2349.Design-a-Number-Container-System/README.md

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [2349.Design a Number Container System][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Design a number container system that can do the following:
75

8-
**Example 1:**
6+
- **Insert** or **Replace** a number at the given index in the system.
7+
- **Return** the smallest index for the given number in the system.
98

10-
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
9+
Implement the `NumberContainers` class:
1410

15-
## 题意
16-
> ...
11+
- `NumberContainers()` Initializes the number container system.
12+
- `void change(int index, int number)` Fills the container at `index` with the `number`. If there is already a number at that `index`, replace it.
13+
- `int find(int number)` Returns the smallest index for the given `number`, or ``-1`` if there is no index that is filled by `number` in the system.
1714

18-
## 题解
15+
**Example 1:**
1916

20-
### 思路1
21-
> ...
22-
Design a Number Container System
23-
```go
2417
```
25-
18+
Input
19+
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
20+
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
21+
Output
22+
[null, -1, null, null, null, null, 1, null, 2]
23+
24+
Explanation
25+
NumberContainers nc = new NumberContainers();
26+
nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
27+
nc.change(2, 10); // Your container at index 2 will be filled with number 10.
28+
nc.change(1, 10); // Your container at index 1 will be filled with number 10.
29+
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
30+
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
31+
nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
32+
nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20.
33+
nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.
34+
```
2635

2736
## 结语
2837

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,87 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
type NumberContainers struct {
6+
indies map[int]int
7+
list map[int][]int
8+
}
9+
10+
func Constructor() NumberContainers {
11+
return NumberContainers{
12+
indies: map[int]int{},
13+
list: map[int][]int{},
14+
}
15+
}
16+
17+
func (this *NumberContainers) Change(index int, number int) {
18+
v, ok := this.indies[index]
19+
this.indies[index] = number
20+
if !ok {
21+
list := this.list[number]
22+
idx := sort.Search(len(list), func(i int) bool {
23+
return list[i] > index
24+
})
25+
if idx == len(list) {
26+
this.list[number] = append(this.list[number], index)
27+
} else {
28+
b := append([]int{index}, list[idx:]...)
29+
a := append(list[:idx], b...)
30+
this.list[number] = a
31+
}
32+
return
33+
}
34+
if v != number {
35+
list := this.list[v]
36+
idx := sort.Search(len(list), func(i int) bool {
37+
return list[i] >= index
38+
})
39+
this.list[v] = append(this.list[v][:idx], this.list[v][idx+1:]...)
40+
41+
list = this.list[number]
42+
idx = sort.Search(len(list), func(i int) bool {
43+
return list[i] > index
44+
})
45+
if idx == len(list) {
46+
this.list[number] = append(this.list[number], index)
47+
} else {
48+
b := append([]int{index}, list[idx:]...)
49+
a := append(list[:idx], b...)
50+
this.list[number] = a
51+
}
52+
}
53+
54+
}
55+
56+
func (this *NumberContainers) Find(number int) int {
57+
v := this.list[number]
58+
if len(v) == 0 {
59+
return -1
60+
}
61+
return v[0]
62+
}
63+
64+
/**
65+
* Your NumberContainers object will be instantiated and called as such:
66+
* obj := Constructor();
67+
* obj.Change(index,number);
68+
* param_2 := obj.Find(number);
69+
*/
70+
71+
type input struct {
72+
name string
73+
index, number int
74+
}
75+
76+
func Solution(inputs []input) []int {
77+
c := Constructor()
78+
ans := make([]int, 0)
79+
for _, in := range inputs {
80+
if in.name == "change" {
81+
c.Change(in.index, in.number)
82+
continue
83+
}
84+
ans = append(ans, c.Find(in.number))
85+
}
86+
return ans
587
}

leetcode/2301-2400/2349.Design-a-Number-Container-System/Solution_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []input
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []input{
17+
{"find", 0, 10},
18+
{"change", 2, 10},
19+
{"change", 1, 10},
20+
{"change", 3, 10},
21+
{"change", 5, 10},
22+
{"find", 0, 10},
23+
{"change", 1, 20},
24+
{"find", 0, 10},
25+
}, []int{-1, 1, 2}},
1926
}
2027

2128
// 开始测试
@@ -30,10 +37,10 @@ func TestSolution(t *testing.T) {
3037
}
3138
}
3239

33-
// 压力测试
40+
// 压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
// 使用案列
44+
// 使用案列
3845
func ExampleSolution() {
3946
}

0 commit comments

Comments
 (0)