Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Find the Number of Distinct Colors Among the Balls

XPChallenge Points: 20
levelLevel: Medium

You have limit + 1 balls labeled 0..limit, all initially uncolored. For each query [x, y], color ball x with color y (overwriting any previous color). After each query, report how many distinct colors are present across all balls (ignore “no color”).

Example 1:

Input: limit = 4 queries = [[1,4],[2,5],[1,3],[3,4]]

Output: [1,2,2,3]

Explanation:

q0: ball1→4 → colors {4} → 1 q1: ball2→5 → colors {4,5} → 2 q2: ball1:4→3 → colors {3,5} → 2 q3: ball3→4 → colors {3,4,5} → 3

Example 2:

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

Output: [1,2,2,3,4]

Explanation:

q0: ball0→1 → {1} → 1 q1: ball1→2 → {1,2} → 2 q2: ball2→2 → {1,2} (2 now on balls 1 & 2) → 2 q3: ball3→4 → {1,2,4} → 3 q4: ball4→5 → {1,2,4,5} → 4

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result