3160. Find the Number of Distinct Colors Among the Balls Medium

@problem@discussion
#Array#Hash Table#Simulation



1/**
2 * [3160] Find the Number of Distinct Colors Among the Balls
3 *
4 * You are given an integer limit and a 2D array queries of size n x 2.
5 * 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.
6 * Return an array result of length n, where result[i] denotes the number of distinct colors after i^th query.
7 * Note that when answering a query, lack of a color will not be considered as a color.
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]</span>
12 * Output: <span class="example-io">[1,2,2,3]</span>
13 * Explanation:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop.gif" style="width: 455px; height: 145px;" />
15 * 
16 * 	After query 0, ball 1 has color 4.
17 * 	After query 1, ball 1 has color 4, and ball 2 has color 5.
18 * 	After query 2, ball 1 has color 3, and ball 2 has color 5.
19 * 	After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]</span>
24 * Output: <span class="example-io">[1,2,2,3,4]</span>
25 * Explanation:
26 * <img alt="" src="https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop2.gif" style="width: 457px; height: 144px;" />
27 * 
28 * 	After query 0, ball 0 has color 1.
29 * 	After query 1, ball 0 has color 1, and ball 1 has color 2.
30 * 	After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.
31 * 	After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.
32 * 	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.
33 * </div>
34 *  
35 * Constraints:
36 * 
37 * 	1 <= limit <= 10^9
38 * 	1 <= n == queries.length <= 10^5
39 * 	queries[i].length == 2
40 * 	0 <= queries[i][0] <= limit
41 * 	1 <= queries[i][1] <= 10^9
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/
47// discuss: https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn query_results(limit: i32, queries: Vec<Vec<i32>>) -> Vec<i32> {
53        vec![]
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_3160() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.