- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1733.java
49 lines (47 loc) · 1.87 KB
/
_1733.java
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
42
43
44
45
46
47
48
49
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashMap;
importjava.util.HashSet;
importjava.util.Map;
importjava.util.Set;
publicclass_1733 {
publicstaticclassSolution1 {
publicintminimumTeachings(intn, int[][] languages, int[][] friendships) {
Map<Integer, Set<Integer>> langMap = newHashMap<>();
for (inti = 0; i < languages.length; i++) {
Set<Integer> set = newHashSet<>();
langMap.put(i + 1, set);
for (intlang : languages[i]) {
set.add(lang);
}
}
boolean[] canCommunicate = newboolean[friendships.length];
for (inti = 1; i <= n; i++) {
for (intj = 0; j < friendships.length; j++) {
intfriend1 = friendships[j][0];
intfriend2 = friendships[j][1];
if (langMap.get(friend1).contains(i) && langMap.get(friend2).contains(i)) {
canCommunicate[j] = true;
}
}
}
intminTeach = friendships.length;
for (inti = 1; i <= n; i++) {
Set<Integer> teach = newHashSet<>();
for (intj = 0; j < friendships.length; j++) {
if (!canCommunicate[j]) {
intfriend1 = friendships[j][0];
intfriend2 = friendships[j][1];
if (!langMap.get(friend1).contains(i)) {
teach.add(friend1);
}
if (!langMap.get(friend2).contains(i)) {
teach.add(friend2);
}
}
}
minTeach = Math.min(minTeach, teach.size());
}
returnminTeach;
}
}
}