Minimum Number of People to Teach
medium · verified · 67.7% accepted · 716 likes · top 74%
array · hash table · greedy
Description
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 i^th user knows, and
- friendships[i] = [ui, vi] denotes a friendship between the users u^i 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.
Example 1:
Example 2:
Solution