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:
<int> queryResults(int limit, vector<vector<int>>& queries) {
vector<int, int> ball;
unordered_map<int, int> colour_count;
unordered_map
<int> ans;
vectorint temp = 0;
for (auto &pair : queries) {
if (ball[pair[0]]) {
--colour_count[ball[pair[0]]];
if (colour_count[ball[pair[0]]] == 0) {
.erase(ball[pair[0]]);
colour_count--temp;
}
}
[pair[0]] = pair[1];
ball
if (!colour_count[pair[1]]) {
++temp;
}
++colour_count[pair[1]];
.push_back(temp);
ans}
return ans;
}
};