-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1733.cpp
More file actions
39 lines (39 loc) · 1.26 KB
/
1733.cpp
File metadata and controls
39 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {
int res = INT_MAX;
int m = languages.size();
int k = friendships.size();
unordered_set<int> needs;
vector<unordered_set<int>> languageSts(m);
vector<bool> learns(m, false);
for (int i = 0; i < m; ++i) {
for (auto& language : languages[i]) {
languageSts[i].insert(language);
}
}
for (int j = 0; j < k; ++j) {
auto friendship = friendships[j];
int u = friendship[0] - 1;
int v = friendship[1] - 1;
bool flag = false;
for (auto& language : languages[u]) {
if (languageSts[v].count(language)) {
flag = true;
break;
}
}
if (flag) continue;
needs.insert(u);
needs.insert(v);
}
for (int learnLanguage = 1; learnLanguage <= n; ++learnLanguage) {
int count = 0;
for (auto& need : needs) {
if (!languageSts[need].count(learnLanguage)) count++;
}
res = min(res, count);
}
return res;
}
};