Minimum Number of People to Teach - Problem
Minimum Number of People to Teach
Imagine a social network where
You are given:
•
•
•
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.
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 friendsYour 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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code