You are given an integer limit and a 2D array queries of size n x 2.
There are limit + 1 balls with distinct labels in the range [0, limit]. Initially, all balls are uncolored. For every query in queries that is of the form [x, y], you mark ball x with the color y. After each query, you need to find the number of distinct colors among the balls.
Return an array result of length n, where result[i] denotes the number of distinct colors afteriᵗʰ query.
Note that when answering a query, lack of a color will not be considered as a color.
fromcollectionsimportdefaultdictclassSolution:defqueryResults(self,limit:int,queries:list[list[int]])->list[int]:answers=[]colors:dict[int,int]={}# colors[idx] means the color of the idx-th ballcounts:dict[int,int]=defaultdict(int)# counts[color] means the number of balls with color `color`foridx,colorinqueries:counts[color]+=1ifidxincolors:old_color=colors[idx]counts[old_color]-=1ifcounts[old_color]==0:delcounts[old_color]colors[idx]=coloranswers.append(len(counts))returnanswers
评论需要 JavaScript。