Skip to content

Commit 0ddf16e

Browse files
authored
Create find-the-number-of-distinct-colors-among-the-balls.cpp
1 parent 78cd278 commit 0ddf16e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
vector<int> queryResults(int limit, vector<vector<int>>& queries) {
8+
vector<int> result(size(queries));
9+
unordered_map<int, int> lookup, cnt;
10+
for (int i = 0; i < size(queries); ++i) {
11+
const int x = queries[i][0], y = queries[i][1];
12+
if (lookup.count(x)) {
13+
if (--cnt[lookup[x]] == 0) {
14+
cnt.erase(lookup[x]);
15+
}
16+
}
17+
lookup[x] = y;
18+
++cnt[lookup[x]];
19+
result[i] = size(cnt);
20+
}
21+
return result;
22+
}
23+
};

0 commit comments

Comments
 (0)