给定一个由N个整数和一个整数K组成的数组A [] ,任务是计算给定数组A的长度K的子序列中存在的不同元素的最小数量。
例子:
Input: A = {3, 1, 3, 2, 3, 4, 5, 4}, K = 4
Output: 2
Explanation: The subsequence of length 4 containing minimum number of distinct elements is {3, 3, 3, 4}, consisting of 2 distinct elements, i.e. {3, 4}.
Input: A = {3, 1, 3, 2, 3, 4, 5, 4}, K = 5
Output: 2
Explanation: The subsequence of length 5 containing minimum number of distinct elements is {3, 3, 3, 4, 4}, consisting of 2 distinct elements, i.e. {3, 4}.
天真的方法:最简单的方法是生成长度为K的所有子序列 对于每个子序列,找出其中存在的不同元素的数量。最后,打印最少数量的不同元素。
时间复杂度: O(K * N K )
辅助空间: O(N)
高效的方法:可以使用哈希优化上述方法。请按照以下步骤解决问题:
- 将所有元素的频率存储在给定数组中,即HashMap中的A [] ,即M。
- 遍历哈希图M,然后将频率推入另一个数组,即V。
- 按降序对数组V排序。
- 将两个变量cnt和len初始化为0,以存储所需的结果和由此形成的子序列的长度。
- 使用变量遍历数组V [] ,说我
- 如果len≥K,则跳出循环。
- 否则,将len的值增加V [i] ,将cnt的值增加1 。
- 完成上述步骤后,打印cnt的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the minimum number
// of distinct elements present in any
// subsequence of length K of the given array
void findMinimumDistinct(int A[], int N, int K)
{
// Stores the frequency
// of each array element
unordered_map mp;
// Traverse the array
for (int i = 0; i < N; i++)
// Update frequency
// of array elements
mp[A[i]]++;
// Store the required result
int count = 0;
// Store the length of the
// required subsequence
int len = 0;
// Store the frequencies
// in decreasing order
vector counts;
// Travere the map
for (auto i : mp)
// Push the frequencies
// into the HashMap
counts.push_back(i.second);
// Sort the array in decreasing order
sort(counts.begin(), counts.end(),
greater());
// Add the elements into the subsequence
// starting from one with highest frequency
for (int i = 0; i < counts.size(); i++) {
// If length of subsequence is >= k
if (len >= K)
break;
len += counts[i];
count++;
}
// Print the result
cout << count;
}
// Driver Code
int main()
{
int A[] = { 3, 1, 3, 2, 3, 4, 5, 4 };
int K = 4;
// Store the size of the array
int N = sizeof(A) / sizeof(A[0]);
// Function Call to count minimum
// number of distinct elmeents
// present in a K-length subsequence
findMinimumDistinct(A, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count the minimum number
// of distinct elements present in any
// subsequence of length K of the given array
static void findMinimumDistinct(int A[], int N, int K)
{
// Stores the frequency
// of each array element
Map mp = new HashMap<>();
// Traverse the array
for(int i = 0; i < N; i++)
// Update frequency
// of array elements
mp.put(A[i], mp.getOrDefault(A[i], 0) + 1);
// Store the required result
int count = 0;
// Store the length of the
// required subsequence
int len = 0;
// Store the frequencies
// in decreasing order
ArrayList counts = new ArrayList<>();
// Travere the map
for(Map.Entry i : mp.entrySet())
// Push the frequencies
// into the HashMap
counts.add(i.getValue());
// Sort the array in decreasing order
Collections.sort(counts, (a, b) -> b - a);
// Add the elements into the subsequence
// starting from one with highest frequency
for(int i = 0; i < counts.size(); i++)
{
// If length of subsequence is >= k
if (len >= K)
break;
len += counts.get(i);
count++;
}
// Print the result
System.out.print(count);
}
// Driver code
public static void main(String[] args)
{
int A[] = { 3, 1, 3, 2, 3, 4, 5, 4 };
int K = 4;
// Store the size of the array
int N = A.length;
// Function Call to count minimum
// number of distinct elmeents
// present in a K-length subsequence
findMinimumDistinct(A, N, K);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
from collections import Counter
# Function to count the minimum number
# of distinct elements present in any
# subsequence of length K of the given array
def findMinimumDistinct(A, N, K):
# Stores the frequency
# of each array element
mp = Counter(A)
# Store the required result
count = 0
# Store the length of the
# required subsequence
length = 0
# Store the frequencies
# in decreasing order
counts = []
# Travere the map
for i in mp:
# Push the frequencies
# into the HashMap
counts.append(mp[i])
# Sort the array in decreasing order
counts = sorted(counts)
counts.reverse()
# Add the elements into the subsequence
# starting from one with highest frequency
for i in range(len(counts)):
# If length of subsequence is >= k
if (length >= K):
break
length += counts[i]
count += 1
# Print the result
print(count)
# Driver Code
A = [3, 1, 3, 2, 3, 4, 5, 4]
K = 4
# Store the size of the array
N = len(A)
# Function Call to count minimum
# number of distinct elmeents
# present in a K-length subsequence
findMinimumDistinct(A, N, K)
# This code is contributed by sudhanshugupta2019a
2
时间复杂度: O(N * log(N))
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。