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 colors among the balls.
Return an array result of length n, where result[i] denotes the number of colors after ith query.
Note that when answering a query, lack of a color will not be considered as a color.
Example 1:
Input: limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]
Output: [1,2,2,3]
Explanation:

Example 2:
Input: limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]
Output: [1,2,2,3,4]
Explanation:

Constraints:
1 <= limit <= 1091 <= n == queries.length <= 105queries[i].length == 20 <= queries[i][0] <= limit1 <= queries[i][1] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the number of distinct colors is straightforward. We will look at each ball one at a time and keep track of which colors we've already seen. By the end, we will know how many unique colors exist.
Here's how the algorithm would work step-by-step:
def find_distinct_colors_brute_force(balls):
seen_colors = []
for ball_color in balls:
# Check if we've already encountered this color
if ball_color not in seen_colors:
# Only add the color if it's new
seen_colors.append(ball_color)
# Count the distinct colors now
number_of_distinct_colors = len(seen_colors)
return number_of_distinct_colorsThe optimal way to solve this problem is to realize that we only need to count how many different colors exist. Using extra memory allows us to track seen colors efficiently. This avoids unnecessary comparisons or re-counting.
Here's how the algorithm would work step-by-step:
def find_the_number_of_distinct_colors(
balls_with_different_colors
):
seen_colors = set()
# We need to check each ball for unique colors
for ball_color in balls_with_different_colors:
# Only add to the set if we haven't seen it before
if ball_color not in seen_colors:
seen_colors.add(ball_color)
# The length of the set is the number of distinct colors
number_of_distinct_colors = len(seen_colors)
return number_of_distinct_colors| Case | How to Handle |
|---|---|
| Null or Empty Input Array | Return 0, as there are no balls, and therefore no distinct colors. |
| Array with a Single Ball | Return 1, as there is only one ball, which represents a single distinct color. |
| Array with maximum possible integer values representing colors | The solution should efficiently handle large integer color values, potentially requiring a hash set or other suitable data structure for tracking seen colors. |
| Array with all balls having the same color | Return 1, as even though there are multiple balls, they are all the same color, so only one distinct color exists. |
| Array with a large number of distinct colors, approaching memory limits | Ensure that the chosen data structure for tracking distinct colors (e.g., HashSet) does not exceed available memory. |
| Input contains negative numbers representing colors | The solution should correctly interpret negative numbers as distinct color values and not treat them as errors or invalid input. |
| Very large array with many duplicate colors | Ensure the solution scales well by using a hash set or similar data structure which provides constant time average complexity for add and contains operations. |
| Integer Overflow if using sum of colors | Avoid using summing of colors for comparison/identification purposes; instead, rely on hash sets or similar distinct value identification techniques. |