diff --git a/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/1.gif b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/1.gif new file mode 100644 index 000000000..fac064945 Binary files /dev/null and b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/1.gif differ diff --git a/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/2.gif b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/2.gif new file mode 100644 index 000000000..438c47562 Binary files /dev/null and b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/2.gif differ diff --git a/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/README.md b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/README.md index d4201f332..33558b434 100755 --- a/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/README.md +++ b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution.go b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution.go index d115ccf5e..1f62ffcc4 100644 --- a/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution.go +++ b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution.go @@ -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 } diff --git a/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution_test.go b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution_test.go index 14ff50eb4..1c22c5786 100644 --- a/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution_test.go +++ b/leetcode/3101-3200/3160.Find-the-Number-of-Distinct-Colors-Among-the-Balls/Solution_test.go @@ -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() { }