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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
# [3160.Find the Number of Distinct Colors Among the Balls][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
You are given an integer `limit` and a 2D array `queries` of size `n x 2`.

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.

Return an array `result` of length `n`, where `result[i]` denotes the number of distinct colors after `ith` query.

**Example 1:**
**Note** that when answering a query, lack of a color will not be considered as a color.

**Example 1:**

![1](./1.gif)

```
Input: a = "11", b = "1"
Output: "100"
Input: limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]

Output: [1,2,2,3]

Explanation:

After query 0, ball 1 has color 4.
After query 1, ball 1 has color 4, and ball 2 has color 5.
After query 2, ball 1 has color 3, and ball 2 has color 5.
After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.gif)

### 思路1
> ...
Find the Number of Distinct Colors Among the Balls
```go
```
Input: limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]

Output: [1,2,2,3,4]

Explanation:

After query 0, ball 0 has color 1.
After query 1, ball 0 has color 1, and ball 1 has color 2.
After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.
After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.
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.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(limit int, queries [][]int) []int {
ans := make([]int, len(queries))
colors := make(map[int]int)
colored := make(map[int]int)
for i, q := range queries {
ball, color := q[0], q[1]
sourceColor, ok := colored[ball]
if ok {
colors[sourceColor]--
if colors[sourceColor] == 0 {
delete(colors, sourceColor)
}
}

colors[color]++
colored[ball] = color
ans[i] = len(colors)
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
limit int
queries [][]int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 4, [][]int{{1, 4}, {2, 5}, {1, 3}, {3, 4}}, []int{1, 2, 2, 3}},
{"TestCase2", 4, [][]int{{0, 1}, {1, 2}, {2, 2}, {3, 4}, {4, 5}}, []int{1, 2, 2, 3, 4}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.limit, c.queries)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.limit, c.queries)
}
})
}
}

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

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