Find the Number of Distinct Colors Among the Balls - Problem

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 all the balls.

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

Note: When answering a query, lack of a color will not be considered as a color.

Input & Output

Example 1 — Basic Coloring
$ Input: limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]
Output: [1,2,2,3]
💡 Note: Initially all balls uncolored. Query [1,4]: ball 1→color 4, distinct colors={4}→1. Query [2,5]: ball 2→color 5, distinct colors={4,5}→2. Query [1,3]: ball 1→color 3, distinct colors={3,5}→2. Query [3,4]: ball 3→color 4, distinct colors={3,4,5}→3.
Example 2 — Same Color Multiple Balls
$ Input: limit = 3, queries = [[0,1],[1,1],[2,1]]
Output: [1,1,1]
💡 Note: All queries use same color 1. After each query: distinct colors={1}→1.
Example 3 — Recoloring Ball
$ Input: limit = 2, queries = [[0,1],[0,2],[1,1]]
Output: [1,1,2]
💡 Note: Query [0,1]: ball 0→color 1, colors={1}→1. Query [0,2]: ball 0→color 2, colors={2}→1. Query [1,1]: ball 1→color 1, colors={1,2}→2.

Constraints

  • 1 ≤ limit ≤ 109
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 2
  • 0 ≤ queries[i][0] ≤ limit
  • 1 ≤ queries[i][1] ≤ 109

Visualization

Tap to expand
Find Distinct Colors Among Balls INPUT Balls [0 to limit=4] 0 1 2 3 4 Queries: [1,4] - Ball 1 gets color 4 [2,5] - Ball 2 gets color 5 [1,3] - Ball 1 gets color 3 [3,4] - Ball 3 gets color 4 Input Values limit = 4 queries = [[1,4],[2,5], [1,3],[3,4]] ALGORITHM STEPS 1 Query [1,4] Ball 1 = color 4 colorCount: {4:1} = 1 2 Query [2,5] Ball 2 = color 5 colorCount: {4:1,5:1} = 2 3 Query [1,3] Ball 1: 4-->3 (recolor) colorCount: {3:1,5:1} = 2 4 Query [3,4] Ball 3 = color 4 colorCount: {3:1,4:1,5:1} but 5 from ball2 = 2 Hash Maps Used ballColor: ball --> color colorCount: color --> count Track colors and their frequency FINAL RESULT Final Ball States: 0 none 1 c=3 2 c=5 3 c=4 After each query: Q1: [1,4] --> 1 Q2: [2,5] --> 2 Q3: [1,3] --> 2 Q4: [3,4] --> 2 OUTPUT [1, 2, 2, 2] OK - Verified Key Insight: Use two hash maps: ballColor (ball --> current color) and colorCount (color --> frequency). When recoloring, decrement old color count. If count becomes 0, that color is no longer distinct. The number of keys with count > 0 in colorCount gives distinct colors. Time: O(n), Space: O(n) TutorialsPoint - Find the Number of Distinct Colors Among the Balls | Hash Map Approach
Asked in
Google 15 Amazon 12 Microsoft 8
8.5K Views
Medium Frequency
~15 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen