给定一个整数N和两个数组A [] [] (表示一个人知道的语言集),以及B [] [] (由M对友谊组成),任务是找到要教的最少人数。单一语言,以便每一对朋友都可以相互交流。
例子:
Input: N = 2, A[][] = {{1}, {2}, {1, 2}}, B[][] = {{1, 2}, {1, 3}, {2, 3}}
Output: 1
Explanation:
One possible way is to teach the 1st person the language 2.
Another possible way is to teach the 2nd person the language 1.
Therefore, the minimum number of languages required to be taught is 1.
Input: N = 4, A[][] = {{2}, {1, 4}, {1, 2}, {3, 4}}, B[][] = {{1, 4}, {1, 2}, {3, 4}, {2, 3}}
Output: 2
Explanation:
One of the possible way is to teach the 1st person the language 3 or 4 and the 3rd person the language 3 or 4.
Therefore, the minimum number of languages required to be taught is 2.
方法:可以使用Set and Map数据结构解决给定问题,从而解决给定问题。
请按照以下步骤解决问题:
- 定义一个函数,例如Check(A [],B []) ,以检查两个排序数组中是否存在任何公共元素:
- 定义两个变量,例如P1和P2,以存储指针。
- 在P1
和P2 迭代。如果A [P1]等于B [P2] ,则返回true。否则,如果A [P1] 一个增量P1。否则,如果A [P1]> B [P2],由1个增量P2。 - 如果以上情况都不满足,则返回false。
- 初始化一个set
,例如S ,然后一个map ,例如mp ,以存储所有无法与朋友交流的人和知道某种特定语言的人的数量。 - 初始化一个变量,例如result ,以存储要教的人数。
- 遍历数组B [] []并通过调用函数Check(B [i] [0],B [i] [1])插入一对人,如果他们之间没有共同语言。
- 遍历一个人知道的语言集S并在Map mp中增加该语言的计数。
- 遍历map
mp并更新结果为result = min(S.size()– it.second,result)。 - 最后,完成上述步骤后,打印结果。
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Function to check if there
// exists any common language
bool check(vector& a, vector& b)
{
// Stores the size of array a[]
int M = a.size();
// Stores the size of array b[]
int N = b.size();
// Pointers
int p1 = 0, p2 = 0;
// Iterate while p1 < M and p2 < N
while (p1 < M && p2 < N) {
if (a[p1] < b[p2])
p1++;
else if (a[p1] > b[p2])
p2++;
else
return true;
}
return false;
}
// Function to count the minimum number
// of people required to be teached
int minimumTeachings(
int N, vector >& languages,
vector > friendships)
{
// Stores the size of array A[][]
int m = languages.size();
// Stores the size of array B[][]
int t = friendships.size();
// Stores all the persons with no
// common languages with their friends
unordered_set total;
// Stores count of languages
unordered_map overall;
// Sort all the languages of
// a person in ascending order
for (int i = 0; i < m; i++)
sort(languages[i].begin(),
languages[i].end());
// Traverse the array B[][]
for (int i = 0; i < t; i++) {
// Check if there is no common
// language between two friends
if (!check(languages[friendships[i][0] - 1],
languages[friendships[i][1] - 1])) {
// Insert the persons in the Set
total.insert(friendships[i][0]);
total.insert(friendships[i][1]);
}
}
// Stores the size of the Set
int s = total.size();
// Stores the count of
// minimum persons to teach
int result = s;
// Traverse the set total
for (auto p : total) {
// Traverse A[p - 1]
for (int i = 0;
i < languages[p - 1].size(); i++)
// Increment count of languages by one
overall[languages[p - 1][i]]++;
}
// Traverse the map
for (auto c : overall)
// Update result
result = min(result, s - c.second);
// Return the result
return result;
}
// Driver Code
int main()
{
int N = 3;
vector > A
= { { 1 }, { 1, 3 }, { 1, 2 }, { 3 } };
vector > B
= { { 1, 4 }, { 1, 2 }, { 3, 4 }, { 2, 3 } };
cout << minimumTeachings(N, A, B) << endl;
return 0;
}
Java
// Java implementation of
// the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to check if there
// exists any common language
static boolean check(int a[], int b[])
{
// Stores the size of array a[]
int M = a.length;
// Stores the size of array b[]
int N = b.length;
// Pointers
int p1 = 0, p2 = 0;
// Iterate while p1 < M and p2 < N
while (p1 < M && p2 < N)
{
if (a[p1] < b[p2])
p1++;
else if (a[p1] > b[p2])
p2++;
else
return true;
}
return false;
}
// Function to count the minimum number
// of people required to be teached
static int minimumTeachings(int N, int languages[][],
int friendships[][])
{
// Stores the size of array A[][]
int m = languages.length;
// Stores the size of array B[][]
int t = friendships.length;
// Stores all the persons with no
// common languages with their friends
HashSet total = new HashSet<>();
// Stores count of languages
HashMap overall = new HashMap<>();
// Sort all the languages of
// a person in ascending order
for(int i = 0; i < m; i++)
Arrays.sort(languages[i]);
// Traverse the array B[][]
for(int i = 0; i < t; i++)
{
// Check if there is no common
// language between two friends
if (!check(languages[friendships[i][0] - 1],
languages[friendships[i][1] - 1]))
{
// Insert the persons in the Set
total.add(friendships[i][0]);
total.add(friendships[i][1]);
}
}
// Stores the size of the Set
int s = total.size();
// Stores the count of
// minimum persons to teach
int result = s;
// Traverse the set total
for(int p : total)
{
// Traverse A[p - 1]
for(int i = 0; i < languages[p - 1].length; i++)
// Increment count of languages by one
overall.put(languages[p - 1][i],
overall.getOrDefault(
languages[p - 1][i], 0) + 1);
}
// Traverse the map
for(int k : overall.keySet())
// Update result
result = Math.min(result, s - overall.get(k));
// Return the result
return result;
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
int A[][] = { { 1 }, { 1, 3 },
{ 1, 2 }, { 3 } };
int B[][] = { { 1, 4 }, { 1, 2 },
{ 3, 4 }, { 2, 3 } };
System.out.println(minimumTeachings(N, A, B));
}
}
// This code is contributed by Kingash
Python3
# Python3 implementation of
# the above approach
# Function to check if there
# exists any common language
def check(a, b):
# Stores the size of array a[]
M = len(a)
# Stores the size of array b[]
N = len(b)
# Pointers
p1 = 0
p2 = 0
# Iterate while p1 < M and p2 < N
while(p1 < M and p2 < N):
if (a[p1] < b[p2]):
p1 += 1
elif(a[p1] > b[p2]):
p2 += 1
else:
return True
return False
# Function to count the minimum number
# of people required to be teached
def minimumTeachings(N, languages, friendships):
# Stores the size of array A[][]
m = len(languages)
# Stores the size of array B[][]
t = len(friendships)
# Stores all the persons with no
# common languages with their friends
total = set()
# Stores count of languages
overall = {}
# Sort all the languages of
# a person in ascending order
for i in range(m):
languages[i].sort(reverse=False)
# Traverse the array B[][]
for i in range(t):
# Check if there is no common
# language between two friends
if(check(languages[friendships[i][0] - 1], languages[friendships[i][1] - 1])==False):
# Insert the persons in the Set
total.add(friendships[i][0])
total.add(friendships[i][1])
# Stores the size of the Set
s = len(total)
# Stores the count of
# minimum persons to teach
result = s
# Traverse the set total
for p in total:
# Traverse A[p - 1]
for i in range(len(languages[p - 1])):
# Increment count of languages by one
if languages[p - 1][i] in overall:
overall[languages[p - 1][i]] += 1
else:
overall[languages[p - 1][i]] = 1
# Traverse the map
for keys,value in overall.items():
# Update result
result = min(result, s - value)
# Return the result
return result
# Driver Code
if __name__ == '__main__':
N = 3
A = [[1],[1, 3],[1, 2],[3]]
B = [[1, 4],[1, 2],[3, 4],[2, 3]]
print(minimumTeachings(N, A, B))
# This code is contributed by ipg2016107.
1
时间复杂度: O(N 2 )
辅助空间: O(N)