New Problem Solution - "Minimum Number of People to Teach"
This commit is contained in:
parent
1483a6e992
commit
5e6f6a8639
@ -21,6 +21,7 @@ LeetCode
|
||||
|1750|[Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/) | [C++](./algorithms/cpp/minimumLengthOfStringAfterDeletingSimilarEnds/MinimumLengthOfStringAfterDeletingSimilarEnds.cpp)|Medium|
|
||||
|1749|[Maximum Absolute Sum of Any Subarray](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/) | [C++](./algorithms/cpp/maximumAbsoluteSumOfAnySubarray/MaximumAbsoluteSumOfAnySubarray.cpp)|Medium|
|
||||
|1748|[Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/) | [C++](./algorithms/cpp/sumOfUniqueElements/SumOfUniqueElements.cpp)|Easy|
|
||||
|1733|[Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/) | [C++](./algorithms/cpp/minimumNumberOfPeopleToTeach/MinimumNumberOfPeopleToTeach.cpp)|Medium|
|
||||
|1732|[Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/) | [C++](./algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp)|Easy|
|
||||
|1605|[Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [C++](./algorithms/cpp/FindValidMatrixGivenRowAndColumnSums/FindValidMatrixGivenRowAndColumnSums.cpp)|Medium|
|
||||
|1573|[Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/) | [C++](./algorithms/cpp/NumberOfWaysToSplitString/NumberOfWaysToSplitString.cpp)|Medium|
|
||||
|
@ -0,0 +1,97 @@
|
||||
// Source : https://leetcode.com/problems/minimum-number-of-people-to-teach/
|
||||
// Author : Hao Chen
|
||||
// Date : 2021-02-17
|
||||
|
||||
/*****************************************************************************************************
|
||||
*
|
||||
* 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 array languages, and an array friendships where:
|
||||
*
|
||||
* There are n languages numbered 1 through n,
|
||||
* languages[i] is the set of languages the ith user knows, and
|
||||
* friendships[i] = [ui, vi] denotes a friendship between the users u
|
||||
* i and vi.
|
||||
*
|
||||
* 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 if x is a friend of y and y is a friend of z,
|
||||
* this doesn't guarantee that x is a friend of z.
|
||||
*
|
||||
* 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 unique
|
||||
* languages[i] contains only unique values
|
||||
******************************************************************************************************/
|
||||
|
||||
class Solution {
|
||||
|
||||
public:
|
||||
bool hasLang(vector<int>& langlist, int lang){
|
||||
for(auto& l : langlist) {
|
||||
if (l == lang) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool canComm(int u, int v, int n, vector<vector<bool>>& langs) {
|
||||
for (int l=0; l<n; l++) {
|
||||
if (langs[u][l] && langs[v][l]) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {
|
||||
int persons = languages.size();
|
||||
//use to check a person know a langauge or not
|
||||
vector<vector<bool>> langs(persons, vector<bool>(n, false));
|
||||
for (int p=0; p<persons; p++) {
|
||||
for(int l=0; l<languages[p].size(); l++) {
|
||||
langs[p][languages[p][l]-1] = true;
|
||||
}
|
||||
}
|
||||
|
||||
int result = persons;
|
||||
vector<vector<bool>> langToTeach(n, vector<bool>(persons, false));
|
||||
for (int l=0; l <n; l++) {
|
||||
int cnt = 0;
|
||||
vector<bool> teach(persons, false);
|
||||
for (auto& fs : friendships) {
|
||||
int u = fs[0] - 1;
|
||||
int v = fs[1] - 1;
|
||||
|
||||
if (canComm(u, v, n, langs)) continue;
|
||||
|
||||
if (langs[u][l]==false && teach[u]==false) {
|
||||
teach[u] = true; cnt++;
|
||||
}
|
||||
if (langs[v][l]==false && teach[v]==false) {
|
||||
teach[v] = true; cnt++;
|
||||
}
|
||||
}
|
||||
result = min(result, cnt);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user