Ctrl K

Colours in Play

Medium
There are balls numbered from 0 to last_label, all of them unpainted at first. Paint jobs come one by one: paints[i] = [ball, color] means ball ball is painted with colour color (its previous colour, if any, is gone). After each paint job, count how many different colours the balls currently show; an unpainted ball adds no colour. Return these counts in the order of the paint jobs.
Example 1
last_label=3, paints=[[0, 7], [1, 7], [0, 2], [1, 2]] → [1, 1, 2, 1]

After each paint the colours in use are {7}, {7}, {7, 2}, {2}, so the answer is [1, 1, 2, 1].

Example 2
last_label=5, paints=[[4, 1], [4, 1], [2, 3]] → [1, 1, 2]

Repainting ball 4 with the same colour changes nothing.

Constraints
  • 1 ≤ last_label ≤ 10⁹
  • 1 ≤ len(paints) ≤ 10⁵
  • 0 ≤ paints[i][0] ≤ last_label
  • 1 ≤ paints[i][1] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHow Far the Rover Got MediumLast Step With a Lit Run MediumFully Dug Relics MediumA Chain of Replacements MediumRows That Match Columns Medium
loading Python…
Input
last_label=3, paints=[[0, 7], [1, 7], [0, 2], [1, 2]]
Expected
[1, 1, 2, 1]

Run checks the first 3 tests in your browser. Submit checks all 49 tests on the server, hidden ones included.