Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions leetcode/2301-2400/2349.Design-a-Number-Container-System/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# [2349.Design a Number Container System][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 number container system that can do the following:

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

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

## 题意
> ...
- `NumberContainers()` Initializes the number container system.
- `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.
- `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.

## 题解
**Example 1:**

### 思路1
> ...
Design a Number Container System
```go
```

Input
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
Output
[null, -1, null, null, null, null, 1, null, 2]

Explanation
NumberContainers nc = new NumberContainers();
nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
nc.change(2, 10); // Your container at index 2 will be filled with number 10.
nc.change(1, 10); // Your container at index 1 will be filled with number 10.
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
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.
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.
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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,87 @@
package Solution

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

type NumberContainers struct {
indies map[int]int
list map[int][]int
}

func Constructor() NumberContainers {
return NumberContainers{
indies: map[int]int{},
list: map[int][]int{},
}
}

func (this *NumberContainers) Change(index int, number int) {
v, ok := this.indies[index]
this.indies[index] = number
if !ok {
list := this.list[number]
idx := sort.Search(len(list), func(i int) bool {
return list[i] > index
})
if idx == len(list) {
this.list[number] = append(this.list[number], index)
} else {
b := append([]int{index}, list[idx:]...)
a := append(list[:idx], b...)
this.list[number] = a
}
return
}
if v != number {
list := this.list[v]
idx := sort.Search(len(list), func(i int) bool {
return list[i] >= index
})
this.list[v] = append(this.list[v][:idx], this.list[v][idx+1:]...)

list = this.list[number]
idx = sort.Search(len(list), func(i int) bool {
return list[i] > index
})
if idx == len(list) {
this.list[number] = append(this.list[number], index)
} else {
b := append([]int{index}, list[idx:]...)
a := append(list[:idx], b...)
this.list[number] = a
}
}

}

func (this *NumberContainers) Find(number int) int {
v := this.list[number]
if len(v) == 0 {
return -1
}
return v[0]
}

/**
* Your NumberContainers object will be instantiated and called as such:
* obj := Constructor();
* obj.Change(index,number);
* param_2 := obj.Find(number);
*/

type input struct {
name string
index, number int
}

func Solution(inputs []input) []int {
c := Constructor()
ans := make([]int, 0)
for _, in := range inputs {
if in.name == "change" {
c.Change(in.index, in.number)
continue
}
ans = append(ans, c.Find(in.number))
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []input
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []input{
{"find", 0, 10},
{"change", 2, 10},
{"change", 1, 10},
{"change", 3, 10},
{"change", 5, 10},
{"find", 0, 10},
{"change", 1, 20},
{"find", 0, 10},
}, []int{-1, 1, 2}},
}

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

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

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