Find the Number of Distinct Colors Among the Balls - Problem

Imagine you have a collection of numbered balls and a magical palette of colors! 🎨

You start with limit + 1 balls, each labeled with a unique number from 0 to limit. Initially, all balls are colorless - waiting to be painted!

You receive a series of painting instructions in the form of queries [x, y], where:

  • x is the ball number to paint
  • y is the color to use

After each painting instruction, you need to count how many distinct colors are currently visible among all the balls. Remember: unpainted balls don't count as having any color!

Goal: Return an array where each element represents the number of distinct colors after processing each query.

Example: If you have balls [0,1,2] and queries [[1,3], [0,3], [2,5]], after the first query you have 1 distinct color (3), after the second you still have 1 distinct color (3), and after the third you have 2 distinct colors (3 and 5).

Input & Output

example_1.py — Basic Color Tracking
$ Input: limit = 3, queries = [[1,4],[0,1],[1,3],[3,4]]
Output: [1,2,2,2]
💡 Note: Ball 1→color 4 (1 distinct), Ball 0→color 1 (2 distinct), Ball 1→color 3 (still 2 distinct: colors 1,3), Ball 3→color 4 (still 2 distinct: colors 1,3 since color 4 reappeared)
example_2.py — Color Replacement
$ Input: limit = 2, queries = [[0,1],[1,2],[0,2]]
Output: [1,2,1]
💡 Note: Ball 0→color 1 (1 distinct), Ball 1→color 2 (2 distinct), Ball 0→color 2 (1 distinct since both balls now have color 2)
example_3.py — Single Ball Multiple Colors
$ Input: limit = 1, queries = [[0,5],[0,8],[0,5]]
Output: [1,1,1]
💡 Note: Only one ball exists, so regardless of which color we paint it, there's always exactly 1 distinct color in the collection

Constraints

  • 1 ≤ limit ≤ 105
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 2
  • 0 ≤ queries[i][0] ≤ limit
  • 1 ≤ queries[i][1] ≤ 109
  • All ball indices are valid and colors are positive integers

Visualization

Tap to expand
🎨 Art Gallery Color ManagementFrame 0Frame 1Frame 2📖 Curator's LogbookColor Usage Tracking:🔴 Red: 1 painting🔵 Blue: 0 paintings🟢 Green: 0 paintingsCurrent Distinct Colors: 1🎯 Efficiency in Action:Traditional Approach: Walk through entire gallery each time ⏱️ O(n×limit)Smart Approach: Update logbook instantly ⚡ O(1) per query1Update CountInstant result!
Understanding the Visualization
1
Gallery Setup
Start with numbered frames (balls), all empty initially
2
Visitor Request
A visitor requests painting frame X with color Y
3
Smart Update
Update your logbook: track color usage efficiently
4
Instant Answer
Provide the current distinct color count immediately
Key Takeaway
🎯 Key Insight: Instead of recounting everything after each change, maintain a smart tracking system that updates incrementally. This transforms an O(n×limit) problem into an O(n) solution!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K 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