In this approach, I used a dictionary ball_colors
to keep track of the latest color assigned to each ball. Also, I added a set
called distinct_colors
to store all unique colors in use.
For each query [x, y]
, I first check if ball x
has already been assigned a color. If it has and the color is different from y
, I decrement the count of the old color in a dictionary color_count
. If the count reaches zero, I remove the old color from distinct_colors
. Then, I update ball x
with the new color y
and update its count in color_count
. Finally, I add y
to distinct_colors
and append the size of distinct_colors
to my result list.