Untitled

 avatar
unknown
plain_text
3 months ago
825 B
4
Indexable
from collections import defaultdict

class Solution:
    def queryResults(self, limit: int, queries: List[List[int]]) -> List[int]:
        res = []
        ball_to_color = {}
        color_freq = defaultdict(int)

        for ball, color in queries:
            prev_ball_color = ball_to_color[ball] if ball in ball_to_color else -1
            ball_to_color[ball] = color
            
            if prev_ball_color != -1 and prev_ball_color != color:
                color_freq[prev_ball_color] -= 1
                if color_freq[prev_ball_color] == 0:
                    del color_freq[prev_ball_color]
            
            if prev_ball_color == -1 or prev_ball_color != color:
                color_freq[color] += 1


            res.append(len(color_freq.keys()))
        
        return res
Editor is loading...
Leave a Comment