Minimum Number of People to Teach: 1733. Minimum Number of People to Teach
Difficulty: Medium
Topics:
Array
,Hash Table
,Greedy
,Biweekly Contest 44
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.Given an integer
n
, an arraylanguages
, and an arrayfriendships
where:
- There are
n
languages numbered1
through <1733. Minimum Number of People to Teach
Difficulty: Medium
Topics:
Array
,Hash Table
,Greedy
,Biweekly Contest 44
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 arraylanguages
, and an arrayfriendships
where:
- There are
n
languages numbered1
throughn
,languages[i]
is the set of languages theith
user knows, andfriendships[i] = [ui, vi]
denotes a friendship between the usersui
andvi
.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 ifx
is a friend ofy
andy
is a friend ofz
, this doesn’t guarantee thatx
is a friend ofz
.Example 1:
- Input: n = 2, languages = [[1],[2],[1,2]], friendships = [[1,2],[1,3],[2,3]]
- Output: 1
- Explanation: You can either teach user 1 the second language or user 2 the first language.
Example 2:
- Input: n = 3, languages = [[2],[1,3],[1,2],[3]], friendships = [[1,4],[1,2],[3,4],[2,3]]
- Output: 2
- Explanation: Teach the third language to users 1 and 3, yielding two users to teach.
Constraints:
2 <= n <= 500
languages.length == m
1 <= m <= 500
1 <= languages[i].length <= n
1 <= languages[i][j] <= n
1 <= ui < vi <= languages.length
1 <= friendships.length <= 500
- All tuples
(ui, vi)
are uniquelanguages[i]
contains only unique valuesHint:
- You can just use brute force and find out for each language the number of users you need to teach
- Note that a user can appear in multiple friendships but you need to teach that user only once
Solution:
We need to determine the minimum number of users to teach a single language such that every pair of friends can communicate through a common language. The solution involves checking each language as a candidate and calculating how many users need to learn it to satisfy all friendships that currently lack a common language.
Approach
- Precompute Language Knowledge: Create a 2D array (
knows
) where each entryknows[i][j]
indicates whether useri
knows languagej
.- Identify Unsatisfied Friendships: For each friendship, check if there exists any common language between the two users. If not, mark both users as needing intervention.
- Calculate Teaching Needs per Language: For each language, count how many users involved in unsatisfied friendships do not already know that language. This count represents the number of users that need to be taught that language to ensure all unsatisfied friendships share that common language.
- Find Minimum Teaching Count: Iterate through all languages and find the minimum count of users that need to be taught.
Let’s implement this solution in PHP: 1733. Minimum Number of People to Teach
<?php /** * @param Integer $n * @param Integer[][] $languages * @param Integer[][] $friendships * @return Integer */ function minimumTeachings($n, $languages, $friendships) { ... ... ... /** * go to ./solution.php */ } // Test cases $n = 2; $languages = [[1],[2],[1,2]]; $friendships = [[1,2],[1,3],[2,3]]; echo minimumTeachings($n, $languages, $friendships) . "\n"; // Output: 1 $n = 3; $languages = [[2],[1,3],[1,2],[3]]; $friendships = [[1,4],[1,2],[3,4],[2,3]]; echo minimumTeachings($n, $languages, $friendships) . "\n"; // Output: 2 ?>
Explanation:
- Precompute Language Knowledge: The code first initializes a 2D array
knows
to store which languages each user knows. This is done by iterating through each user’s known languages and marking them in the array.- Identify Unsatisfied Friendships: For each friendship, the code checks if there is any common language between the two users. If no common language is found, both users are added to a set of users involved in unsatisfied friendships.
- Calculate Teaching Needs: For each language, the code counts how many users in the set of unsatisfied users do not already know that language. This count represents the number of users that need to be taught that language to resolve all unsatisfied friendships.
- Find Minimum Teaching Count: The code iterates through all languages and keeps track of the minimum count of users that need to be taught. This minimum value is returned as the result.
This approach efficiently narrows down the problem to checking each language individually and determining the optimal language to teach to minimize the number of users needing instruction. The complexity is manageable due to the constraints, making it a practical solution.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me: