diff --git a/3160. Find the Number of Distinct Colors Among the Balls b/3160. Find the Number of Distinct Colors Among the Balls new file mode 100644 index 0000000..024c82a --- /dev/null +++ b/3160. Find the Number of Distinct Colors Among the Balls @@ -0,0 +1,25 @@ +class Solution { +public: + vector queryResults(int limit, vector>& queries) { + int n = queries.size(); + vector res(n, -1); + unordered_map colormp; + unordered_map ballmp; + + for (int i = 0; i < n; i++) { + int ball = queries[i][0]; + int color = queries[i][1]; + if (ballmp.find(ball) != ballmp.end()) { + int prevcolor = ballmp[ball]; + colormp[prevcolor]--; + if (colormp[prevcolor] == 0) { + colormp.erase(prevcolor); + } + } + ballmp[ball] = color; + colormp[color]++; + res[i] = colormp.size(); + } + return res; + } +};