|
| 1 | +/** |
| 2 | + * Problem: Minimum Number of People to Teach |
| 3 | + * Link: https://leetcode.com/problems/minimum-number-of-people-to-teach/ |
| 4 | + * |
| 5 | + * You are given n languages numbered from 1 to n, and for each user, a list of languages they know. |
| 6 | + * Some pairs of users are friends but might not share a common language. |
| 7 | + * You can teach one language to some users so that all friends can communicate. |
| 8 | + * Return the minimum number of users you need to teach. |
| 9 | + * |
| 10 | + * Approach: |
| 11 | + * 1. Build a mapping from each user to the set of languages they know. |
| 12 | + * 2. Find all problematic friendship pairs (where no shared language exists). |
| 13 | + * 3. Collect all unique users involved in problematic pairs. |
| 14 | + * 4. For every language from 1 to n: |
| 15 | + * - Count how many users in the problematic set do NOT know that language. |
| 16 | + * - Keep track of the minimum such count. |
| 17 | + * 5. Return the minimum number of people to teach. |
| 18 | + * |
| 19 | + * Time Complexity: O(n × u), where: |
| 20 | + * - n = number of languages (usually small, ≤ 500) |
| 21 | + * - u = number of unique problematic users (≤ 500) |
| 22 | + * |
| 23 | + * Space Complexity: O(u + p): |
| 24 | + * - u: Unique problematic users (≤ 500) |
| 25 | + * - p: Problematic friendship pairs (≤ 10⁴) |
| 26 | + */ |
| 27 | + |
| 28 | +class Solution { |
| 29 | + public int minimumTeachings(int n, int[][] languages, int[][] friendships) { |
| 30 | + // Map user to their known languages |
| 31 | + HashMap<Integer, Set<Integer>> userLanguage = new HashMap<>(); |
| 32 | + for (int i = 0; i < languages.length; i++) { |
| 33 | + HashSet<Integer> set = new HashSet<>(); |
| 34 | + for (int lang : languages[i]) { |
| 35 | + set.add(lang); |
| 36 | + } |
| 37 | + userLanguage.put(i + 1, set); // User IDs are 1-based |
| 38 | + } |
| 39 | + |
| 40 | + // Find problematic friendships |
| 41 | + HashSet<Integer> uniqueProblematicUsers = new HashSet<>(); |
| 42 | + |
| 43 | + for (int[] pair : friendships) { |
| 44 | + Set<Integer> firstUserLangs = userLanguage.get(pair[0]); |
| 45 | + Set<Integer> secondUserLangs = userLanguage.get(pair[1]); |
| 46 | + |
| 47 | + boolean hasCommonLang = firstUserLangs.stream().anyMatch(secondUserLangs::contains); |
| 48 | + |
| 49 | + if (!hasCommonLang) { |
| 50 | + uniqueProblematicUsers.add(pair[0]); |
| 51 | + uniqueProblematicUsers.add(pair[1]); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Try every language and compute minimum teaching effort |
| 56 | + int minToTeach = Integer.MAX_VALUE; |
| 57 | + |
| 58 | + for (int lang = 1; lang <= n; lang++) { |
| 59 | + int teachCount = 0; |
| 60 | + for (int user : uniqueProblematicUsers) { |
| 61 | + Set<Integer> knownLanguages = userLanguage.get(user); |
| 62 | + if (!knownLanguages.contains(lang)) { |
| 63 | + teachCount++; |
| 64 | + } |
| 65 | + } |
| 66 | + minToTeach = Math.min(minToTeach, teachCount); |
| 67 | + } |
| 68 | + |
| 69 | + return minToTeach; |
| 70 | + } |
| 71 | +} |
0 commit comments