On a social network consisting of m users and some friendships between users, two users can communicate with each other if they know a common language.
You are given an integer n, an array languages, and an array friendships
where:
There are n languages numbered 1 through n,
languages[i] is the set of languages the ith user knows, and
friendships[i] = [ui, vi] denotes a friendship between the users ui and vi.
You can choose one language and teach it to some users so that all friends can communicate with each other. Return the _minimum _number of users you need to teach.
Note that friendships are not transitive, meaning if x is a friend of y
and y is a friend of z, this doesn’t guarantee that x is a friend of
z.
Input: n =2, languages =[[1],[2],[1,2]], friendships =[[1,2],[1,3],[2,3]]Output: 1Explanation: You can either teach user 1 the second language or user 2 the first language.
Input: n =3, languages =[[2],[1,3],[1,2],[3]], friendships =[[1,4],[1,2],[3,4],[2,3]]Output: 2Explanation: Teach the third language to users 1 and 3, yielding two users to teach.
For each friendship, if the two users do not share a language, we must teach one language to at least one of them. To minimize the number of people to teach, try each language and count how many users need to learn it so all friendships are covered.
#include<vector>#include<unordered_set>usingnamespace std;
classSolution {
public:int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {
int m = languages.size();
vector<unordered_set<int>> knows(m);
for (int i =0; i < m; ++i)
for (int l : languages[i]) knows[i].insert(l);
unordered_set<int> candidates;
for (auto& f : friendships) {
int u = f[0] -1, v = f[1] -1;
bool ok = false;
for (int l : knows[u]) if (knows[v].count(l)) { ok = true; break; }
if (!ok) { candidates.insert(u); candidates.insert(v); }
}
int res = m;
for (int l =1; l <= n; ++l) {
int cnt =0;
for (int u : candidates) if (!knows[u].count(l)) cnt++;
res = min(res, cnt);
}
return res;
}
};
import java.util.*;
classSolution {
publicintminimumTeachings(int n, List<List<Integer>> languages, List<List<Integer>> friendships) {
int m = languages.size();
List<Set<Integer>> knows =new ArrayList<>();
for (List<Integer> lang : languages) knows.add(new HashSet<>(lang));
Set<Integer> candidates =new HashSet<>();
for (List<Integer> f : friendships) {
int u = f.get(0) - 1, v = f.get(1) - 1;
boolean ok =false;
for (int l : knows.get(u)) if (knows.get(v).contains(l)) { ok =true; break; }
if (!ok) { candidates.add(u); candidates.add(v); }
}
int res = m;
for (int l = 1; l <= n; ++l) {
int cnt = 0;
for (int u : candidates) if (!knows.get(u).contains(l)) cnt++;
res = Math.min(res, cnt);
}
return res;
}
}
classSolution {
funminimumTeachings(n: Int, languages: List<List<Int>>, friendships: List<List<Int>>): Int {
val m = languages.size
val knows = languages.map { it.toSet() }
val candidates = mutableSetOf<Int>()
for (f in friendships) {
val u = f[0] - 1; val v = f[1] - 1if (knows[u].intersect(knows[v]).isEmpty()) {
candidates.add(u); candidates.add(v)
}
}
var res = m
for (l in1..n) {
var cnt = 0for (u in candidates) if (!knows[u].contains(l)) cnt++ res = minOf(res, cnt)
}
return res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defminimumTeachings(self, n: int, languages: list[list[int]], friendships: list[list[int]]) -> int:
knows = [set(l) for l in languages]
candidates = set()
for u, v in friendships:
u -=1; v -=1if knows[u].isdisjoint(knows[v]):
candidates.add(u); candidates.add(v)
res = len(languages)
for l in range(1, n +1):
cnt = sum(1for u in candidates if l notin knows[u])
res = min(res, cnt)
return res
use std::collections::HashSet;
impl Solution {
pubfnminimum_teachings(n: i32, languages: Vec<Vec<i32>>, friendships: Vec<Vec<i32>>) -> i32 {
let m = languages.len();
let knows: Vec<HashSet<i32>>= languages.iter().map(|l| l.iter().copied().collect()).collect();
letmut candidates = HashSet::new();
for f in friendships {
let u = f[0] -1; let v = f[1] -1;
if knows[u].is_disjoint(&knows[v]) {
candidates.insert(u asi32); candidates.insert(v asi32);
}
}
letmut res = m asi32;
for l in1..=n {
letmut cnt =0;
for&u in&candidates {
if!knows[u asusize].contains(&l) { cnt +=1; }
}
res = res.min(cnt);
}
res
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
minimumTeachings(n: number, languages: number[][], friendships: number[][]):number {
constm=languages.length;
constknows=languages.map(l=>newSet(l));
constcandidates=newSet<number>();
for (const [u, v] offriendships) {
constu0=u-1, v0=v-1;
constok= [...knows[u0]].some(l=>knows[v0].has(l));
if (!ok) { candidates.add(u0); candidates.add(v0); }
}
letres=m;
for (letl=1; l<=n; ++l) {
letcnt=0;
for (constuofcandidates) if (!knows[u].has(l)) cnt++;
res= Math.min(res, cnt);
}
returnres;
}
}