minimum-number-of-people-to-teach
https://leetcode-cn.com/problems/minimum-number-of-people-to-teach/
一开始感觉像是并查集,,,,但是感觉也没有办法冰茶,,,,,
贪心算法:先找出所有没有共同语言的好友,然后统计出这些人里用的最多的语言
用人数-语言数即答案
为啥说这是个贪心呢,因为人数最多的语言,必然是需要最少的人学会的
class Solution {
public:
bool is_com(vector<vector<int>>& languages,int i,int j){
vector<int> lan1=languages[i-1];
vector<int> lan2=languages[j-1];
int size1=lan1.size();
int size2=lan2.size();
for (int k = 0; k < size1; ++k) {
for (int l = 0; l < size2; ++l) {
if (lan1[k] == lan2[l])
return true;
}
}
return false;
}
int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {
unordered_map<int,int> mp1;
unordered_map<int ,int> mp2;
int len=friendships.size();
for (int i = 0; i < len; ++i) {
if(!is_com(languages,friendships[i][0],friendships[i][1])){
mp1[friendships[i][0]]++;
mp1[friendships[i][1]]++;
}
}
for(auto p:mp1){
for(auto lan:languages[p.first-1]){
mp2[lan]++;
}
}
int most=0;
for(auto l:mp2){
most=max(most,l.second);
}
int si=mp1.size();
int ans=si-most;
return ans;
}
};