|
| 1 | +3160\. Find the Number of Distinct Colors Among the Balls |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +You are given an integer `limit` and a 2D array `queries` of size `n x 2`. |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +Return an array `result` of length `n`, where `result[i]` denotes the number of distinct colors _after_ <code>i<sup>th</sup></code> query. |
| 10 | + |
| 11 | +**Note** that when answering a query, lack of a color _will not_ be considered as a color. |
| 12 | + |
| 13 | +**Example 1:** |
| 14 | + |
| 15 | +**Input:** limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]] |
| 16 | + |
| 17 | +**Output:** [1,2,2,3] |
| 18 | + |
| 19 | +**Explanation:** |
| 20 | + |
| 21 | + |
| 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. |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +**Input:** limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]] |
| 31 | + |
| 32 | +**Output:** [1,2,2,3,4] |
| 33 | + |
| 34 | +**Explanation:** |
| 35 | + |
| 36 | +**** |
| 37 | + |
| 38 | +* After query 0, ball 0 has color 1. |
| 39 | +* After query 1, ball 0 has color 1, and ball 1 has color 2. |
| 40 | +* After query 2, ball 0 has color 1, and balls 1 and 2 have color 2. |
| 41 | +* After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4. |
| 42 | +* 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. |
| 43 | + |
| 44 | +**Constraints:** |
| 45 | + |
| 46 | +* <code>1 <= limit <= 10<sup>9</sup></code> |
| 47 | +* <code>1 <= n == queries.length <= 10<sup>5</sup></code> |
| 48 | +* `queries[i].length == 2` |
| 49 | +* `0 <= queries[i][0] <= limit` |
| 50 | +* <code>1 <= queries[i][1] <= 10<sup>9</sup></code> |
0 commit comments