find-the-number-of-distinct-colors-among-the-balls 1.0.0
Find the Number of Distinct Colors Among the Balls
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

vector< int > queryResults (int limit, vector< vector< int > > &queries)
 

Detailed Description

Definition at line 5 of file main.cpp.

Member Function Documentation

◆ queryResults()

vector< int > Solution::queryResults ( int  limit,
vector< vector< int > > &  queries 
)
inline

Definition at line 8 of file main.cpp.

9 {
10 limit = limit; // not needed
11 vector<int> result;
12 unordered_map<int, int> balls;
13 unordered_map<int, int> colors;
14 unordered_set<int> distinct_colors;
15
16 for (const auto& query : queries)
17 {
18 int b = query[0], c = query[1];
19
20 // ball already painted
21 if (balls.count(b))
22 {
23 int old_color = balls[b];
24 if (--colors[old_color] == 0)
25 distinct_colors.erase(old_color);
26 }
27
28 // assigning new color
29 balls[b] = c;
30 if (++colors[c] == 1)
31 distinct_colors.insert(c);
32
33 result.push_back(distinct_colors.size());
34 }
35
36 return result;
37 }

Referenced by main().


The documentation for this class was generated from the following file: