Learning language
You are given n employees and m approved official languages (numbered 1 to m). You are also given a 2D dynamic array languages where languages[i] is a list of languages the i-th employee currently knows. This list can be empty.
The employees want to be able to communicate with each other. Two employees can communicate directly if they share at least one common language. They can also communicate indirectly if other employees can act as translators (e.g., Employee A speaks to B, who translates for C).
An employee can learn any new language for a cost of 1. Find the minimum total cost required so that every employee can communicate with every other employee (either directly or indirectly).
Example 1:
Input: n = 5, m = 5, languages = [ [2], [2, 3], [3, 4], [4, 5], [5] ]
Output: 0
Explanation:
Everyone is already connected in a single communication chain (1 connects to 2, 2 connects to 3, etc.). No one needs to learn a new language.
Example 2:
Input: n = 3, m = 3, languages = [ [ ], [ ], [ ] ]
Output: 3
Explanation: Absolutely nobody in the company knows any languages. Since there is no baseline language to connect them, every single employee must pay to learn at least one common language (for instance, language 1). The total cost is 3.
Now Your Turn!
Pick the correct output for the given inputInput:n = 2, m = 2, languages = [ [2 ], [ ] ]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 <= n, m <= 100
- 0 <= languages[i].length <= m
- 1 <= languages[i][j] <= m
- All languages in languages[i] are unique for a given employee.