Maintain the number of distinct colors after a sequence of ball recoloring operations.
You are given balls labeled from $1n$, and each ball initially has no color. You then receive a list of queries, where each query recolors one ball with a given color.
After every recoloring operation, report how many distinct colors are currently present among all balls.
If a ball is recolored, its previous color is replaced by the new one. Balls with no assigned color are ignored when counting distinct colors.
([ball, color]) meaning ball ball is recolored to color.Example 1
Input
n = 4 queries = [[1, 5], [2, 5], [1, 3], [4, 2]]
Output
[1, 1, 2, 3]
Explanation
After each query, the distinct color sets are:
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.