forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_1733.java
49 lines (47 loc) · 1.85 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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class _1733 {
public static class Solution1 {
public int minimumTeachings(int n, int[][] languages, int[][] friendships) {
Map<Integer, Set<Integer>> langMap = new HashMap<>();
for (int i = 0; i < languages.length; i++) {
Set<Integer> set = new HashSet<>();
langMap.put(i + 1, set);
for (int lang : languages[i]) {
set.add(lang);
}
}
boolean[] canCommunicate = new boolean[friendships.length];
for (int i = 1; i <= n; i++) {
for (int j = 0; j < friendships.length; j++) {
int friend1 = friendships[j][0];
int friend2 = friendships[j][1];
if (langMap.get(friend1).contains(i) && langMap.get(friend2).contains(i)) {
canCommunicate[j] = true;
}
}
}
int minTeach = friendships.length;
for (int i = 1; i <= n; i++) {
Set<Integer> teach = new HashSet<>();
for (int j = 0; j < friendships.length; j++) {
if (!canCommunicate[j]) {
int friend1 = friendships[j][0];
int friend2 = 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());
}
return minTeach;
}
}
}