3160: Find-the-Number-of-Distinct-Colors-Among-the-Balls
Medium

I am not going to lie, this question spoke to me and told me to use some hashmaps; two to be specific.

The unordered_maps that I had to initalise were ball and colour_count:

Then, I just iterated through the queries, maintaining a temp variable to keep track of the number of unique colours present:

Code:

class Solution {
public:
    vector<int> queryResults(int limit, vector<vector<int>>& queries) {
        unordered_map<int, int> ball;
        unordered_map<int, int> colour_count;

        vector<int> ans;
        int temp = 0;
        for (auto &pair : queries) {
            if (ball[pair[0]]) {
                --colour_count[ball[pair[0]]];
                if (colour_count[ball[pair[0]]] == 0) {
                    colour_count.erase(ball[pair[0]]);
                    --temp;
                }
            }
            ball[pair[0]] = pair[1];

            if (!colour_count[pair[1]]) {
                ++temp;
            } 
            ++colour_count[pair[1]];

            ans.push_back(temp);
        }
        return ans;
    }
};

Complexity: