1
+ // Runtime: 310 ms (Top 93.88%) | Memory: 61.5 MB (Top 78.57%)
1
2
class Solution {
2
3
public:
3
4
bool checkinvalid ( int a, int b, vector<vector<int >>& languages){ // helper func., checks if a and b can communicate or not
@@ -7,18 +8,18 @@ class Solution {
7
8
}
8
9
return true ;
9
10
}
10
-
11
- int minimumTeachings (int n, vector<vector<int >>& languages, vector<vector<int >>& friendships) {
11
+
12
+ int minimumTeachings (int n, vector<vector<int >>& languages, vector<vector<int >>& friendships) {
12
13
int m= languages.size ();
13
14
vector<vector<int >> g (m+1 , vector<int >(0 )); // graph to store adjacency list with people as nodes & friendships as edges
14
15
// create graph
15
16
for ( int i=0 ;i<friendships.size ();i++){
16
17
g[friendships[i][0 ]].push_back (friendships[i][1 ]);
17
18
g[friendships[i][1 ]].push_back (friendships[i][0 ]);
18
19
}
19
-
20
+
20
21
vector<int > voters (m+1 ,0 ); // to store people who cant communicate with atleast a friend
21
- int voterscount=0 ;
22
+ int voterscount=0 ;
22
23
for ( int i=1 ;i<m+1 ;i++){
23
24
for ( int j=0 ;j<g[i].size ();j++){
24
25
if ( checkinvalid (i, g[i][j], languages)){ // ith can vote
@@ -28,14 +29,14 @@ class Solution {
28
29
}
29
30
}
30
31
}
31
- unordered_map<int ,int > mappy; // language -> votes
32
- int maxvotes=0 ;
33
-
32
+ unordered_map<int ,int > mappy; // language -> votes
33
+ int maxvotes=0 ;
34
+
34
35
for ( int i=1 ;i<m+1 ;i++){
35
- if (voters[i]==1 ){
36
+ if (voters[i]==1 ){
36
37
for ( int j: languages[i-1 ]){
37
38
mappy[j]++;
38
-
39
+
39
40
if (maxvotes<mappy[j]){
40
41
maxvotes= mappy[j];
41
42
}
@@ -44,4 +45,4 @@ class Solution {
44
45
}
45
46
return voterscount - maxvotes; // # who need to be taught the language
46
47
}
47
- };
48
+ };
0 commit comments