-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-21.java
More file actions
41 lines (39 loc) · 1.14 KB
/
leet-code-q-21.java
File metadata and controls
41 lines (39 loc) · 1.14 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
40
41
import java.util.*;
class Solution {
public int minimumTeachings(int n, int[][] languages, int[][] friendships) {
Set<Integer> need = new HashSet<>();
for (int[] p : friendships) {
int u = p[0] - 1, v = p[1] - 1;
boolean ok = false;
for (int x : languages[u]) {
for (int y : languages[v]) {
if (x == y) {
ok = true;
break;
}
}
if (ok) break;
}
if (!ok) {
need.add(u);
need.add(v);
}
}
int ans = languages.length + 1;
for (int i = 1; i <= n; ++i) {
int cans = 0;
for (int v : need) {
boolean found = false;
for (int c : languages[v]) {
if (c == i) {
found = true;
break;
}
}
if (!found) cans++;
}
ans = Math.min(ans, cans);
}
return ans;
}
}