Skip to content

Commit 0a1b5bd

Browse files
authored
Create 3160. Find the Number of Distinct Colors Among the Balls (#709)
2 parents 5a20532 + a083372 commit 0a1b5bd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<int> queryResults(int limit, vector<vector<int>>& queries) {
4+
int n = queries.size();
5+
vector<int> res(n, -1);
6+
unordered_map<int, int> colormp;
7+
unordered_map<int, int> ballmp;
8+
9+
for (int i = 0; i < n; i++) {
10+
int ball = queries[i][0];
11+
int color = queries[i][1];
12+
if (ballmp.find(ball) != ballmp.end()) {
13+
int prevcolor = ballmp[ball];
14+
colormp[prevcolor]--;
15+
if (colormp[prevcolor] == 0) {
16+
colormp.erase(prevcolor);
17+
}
18+
}
19+
ballmp[ball] = color;
20+
colormp[color]++;
21+
res[i] = colormp.size();
22+
}
23+
return res;
24+
}
25+
};

0 commit comments

Comments
 (0)