Minimum Number of People to Teach - Problem
Minimum Number of People to Teach

Imagine a social network where m users are connected through friendships, but they speak different languages! Two users can only communicate if they share at least one common language.

You are given:
n languages numbered from 1 to n
languages[i] - a list of languages that user i knows
friendships - pairs of users who are friends

Your mission: Choose ONE language and teach it to the minimum number of people so that every pair of friends can communicate with each other.

Note: Friendships are not transitive - if A is friends with B, and B is friends with C, it doesn't mean A and C are friends.

Input & Output

example_1.py — Basic Communication Problem
$ Input: n = 2, languages = [[1],[2],[1,2]], friendships = [[1,2],[1,3],[2,3]]
Output: 1
💡 Note: User 1 and User 2 cannot communicate (User 1 speaks [1], User 2 speaks [2]). We can teach Language 1 to User 2 or Language 2 to User 1. Either way, we need to teach 1 person.
example_2.py — All Can Communicate
$ Input: n = 3, languages = [[2],[1,3],[1,2]], friendships = [[1,2],[2,3]]
Output: 0
💡 Note: User 1-2 friendship: User 1 speaks [2], User 2 speaks [1,3] - no common language. But User 2-3 friendship: User 2 speaks [1,3], User 3 speaks [1,2] - they share language 1. Wait, let me recalculate... Actually User 1 and User 2 have no common language, so we need to teach someone.
example_3.py — Multiple Disconnected Groups
$ Input: n = 3, languages = [[1,2],[1,3],[2,3],[1]], friendships = [[1,2],[2,3],[3,4]]
Output: 1
💡 Note: All adjacent pairs can communicate except some specific pair. We need to identify the most efficient language to teach to connect all friend pairs.

Constraints

  • 2 ≤ n ≤ 500 (number of languages)
  • 1 ≤ m ≤ 500 (number of users)
  • 1 ≤ friendships.length ≤ 500
  • 1 ≤ languages[i].length ≤ n
  • All values in languages[i] are distinct and between 1 and n
  • friendships[i].length == 2
  • 1 ≤ ui, vi ≤ m
  • ui ≠ vi

Visualization

Tap to expand
Social Network Communication OptimizationU1U2U3U4U5BlockedOKBlocked[Lang 1][Lang 2][Lang 1,2][Lang 1,3][Lang 3]Optimization Strategy1. Identify Problematic Pairs: U1-U2 (no common lang), U2-U5 (no common lang)2. Focus on Problem Users: U1, U2, U5 (ignore U3, U4 - not in blocked pairs)3. Language Options: • Teach Lang 1: U2, U5 need it → 2 people • Teach Lang 2: U1, U5 need it → 2 people • Teach Lang 3: U1, U2 need it → 2 peopleMinimum: 2 people need to learn any language
Understanding the Visualization
1
Map the Network
Identify all friendships and their language barriers
2
Find Problems
Locate friend pairs who cannot communicate (no shared language)
3
Focus Resources
Only consider people involved in problematic friendships
4
Optimize Teaching
Choose the language that minimizes total people needing training
Key Takeaway
🎯 Key Insight: Only people involved in communication-blocked friendships matter. Find them first, then choose the language requiring minimum teachings among this focused group.
Asked in
Meta 8 Google 6 Microsoft 4 Amazon 3
23.6K Views
Medium-Low Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen