Skip to content

Commit af7c3c7

Browse files
committed
Add solution and test-cases for problem 3160
1 parent d569e0c commit af7c3c7

File tree

5 files changed

+63
-26
lines changed

5 files changed

+63
-26
lines changed
Loading
Loading

leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [3160.Find the Number of Distinct Colors Among the Balls][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+
You are given an integer `limit` and a 2D array `queries` of size `n x 2`.
5+
6+
There are `limit + 1` balls with **distinct** labels in the range `[0, limit]`. Initially, all balls are uncolored. For every query in `queries` that is of the form `[x, y]`, you mark ball `x` with the color `y`. After each query, you need to find the number of **distinct** colors among the balls.
7+
8+
Return an array `result` of length `n`, where `result[i]` denotes the number of distinct colors after `ith` query.
79

8-
**Example 1:**
10+
**Note** that when answering a query, lack of a color will not be considered as a color.
11+
12+
**Example 1:**
13+
14+
![1](./1.gif)
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]
18+
19+
Output: [1,2,2,3]
20+
21+
Explanation:
22+
23+
After query 0, ball 1 has color 4.
24+
After query 1, ball 1 has color 4, and ball 2 has color 5.
25+
After query 2, ball 1 has color 3, and ball 2 has color 5.
26+
After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.
1327
```
1428

15-
## 题意
16-
> ...
29+
**Example 2:**
1730

18-
## 题解
31+
![2](./2.gif)
1932

20-
### 思路1
21-
> ...
22-
Find the Number of Distinct Colors Among the Balls
23-
```go
2433
```
34+
Input: limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]
35+
36+
Output: [1,2,2,3,4]
2537
38+
Explanation:
39+
40+
After query 0, ball 0 has color 1.
41+
After query 1, ball 0 has color 1, and ball 1 has color 2.
42+
After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.
43+
After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.
44+
After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5.
45+
```
2646

2747
## 结语
2848

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(limit int, queries [][]int) []int {
4+
ans := make([]int, len(queries))
5+
colors := make(map[int]int)
6+
colored := make(map[int]int)
7+
for i, q := range queries {
8+
ball, color := q[0], q[1]
9+
sourceColor, ok := colored[ball]
10+
if ok {
11+
colors[sourceColor]--
12+
if colors[sourceColor] == 0 {
13+
delete(colors, sourceColor)
14+
}
15+
}
16+
17+
colors[color]++
18+
colored[ball] = color
19+
ans[i] = len(colors)
20+
}
21+
return ans
522
}

leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
limit int
14+
queries [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", 4, [][]int{{1, 4}, {2, 5}, {1, 3}, {3, 4}}, []int{1, 2, 2, 3}},
18+
{"TestCase2", 4, [][]int{{0, 1}, {1, 2}, {2, 2}, {3, 4}, {4, 5}}, []int{1, 2, 2, 3, 4}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.limit, c.queries)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.limit, c.queries)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)