C++ | Commented
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {\n        int m = languages.size();\n        \n        // Convert languages to sets for easier lookups\n        vector<unordered_set<int>> langSet(m);\n        for (int i = 0; i < m; ++i) {\n            langSet[i] = unordered_set<int>(languages[i].begin(), languages[i].end());\n        }\n        \n        // Find pairs that cannot communicate\n        unordered_set<int> cannotTalk;\n        for (auto& f : friendships) {\n            int u = f[0] - 1, v = f[1] - 1; // Convert to 0-indexed\n            bool canCommunicate = false;\n            for (int lang : languages[u]) {\n                if (langSet[v].count(lang)) {\n                    canCommunicate = true;\n                    break;\n                }\n            }\n            if (!canCommunicate) {\n                cannotTalk.insert(u);\n                cannotTalk.insert(v);\n            }\n        }\n        \n        // If all pairs can communicate, no teaching needed\n        if (cannotTalk.empty()) return 0;\n        \n        // Count the frequency of each language among the users who cannot communicate\n        unordered_map<int, int> freq;\n        for (int user : cannotTalk) {\n            for (int lang : languages[user]) {\n                freq[lang]++;\n            }\n        }\n        \n        // Find the language with the maximum frequency\n        int maxFreq = 0;\n        for (auto& [lang, count] : freq) {\n            maxFreq = max(maxFreq, count);\n        }\n        \n        return cannotTalk.size() - maxFreq;        \n    }\n};\n```
['C++']