最大化可以从 Array 中选择的 K 个唯一元素的数量
给定一个大小为N的数组arr[]和一个大小为M的查询数组Q[] ,其中Q[i]定义了必须从数组arr[] 中选择的唯一元素的数量。查找可以为每个查询选择的最大元素数的任务。
例子:
Input: arr[ ] = {30, 31, 32, 33, 32, 32, 31, 30, 30, 32}, Q[ ] = {1, 3}
Output: 4 9
Explanation:
Frequency of 32 is 4, 30 is 3, 31 is 2 and 33 is 1.
For Q[0](=1), pick all 32. Hence count = 4.
For Q[1](=3), pick all 32, 30, and 31. Hence count = 4 + 3 + 2 = 9.
Input: arr[ ] = {22, 35, 22, 22, 35}, Q[ ] = {4, 5, 1}
Output: 5 5 3
方法:这个问题可以通过将每个元素的频率存储在地图中来解决。请按照以下步骤解决此问题:
- 初始化一个映射数据结构,比如映射并遍历数组 arr[],然后将数组arr[]的每个元素的频率存储在Map 中。
- 将Map的所有值存储在一个数组中,例如Cum_freq[],然后按降序对数组Cum_freq[]进行排序。
- 使用变量i在范围[1, size of Cum_freq[]] 中迭代并更新 Cum_freq[i] 中Cum_freq[i] + Cum_freq[i-1]的值。
- 使用变量i在[0, size of queries[]]范围内迭代:
- 如果查询[i] >= Cum_freq[] 的大小,则打印N。
- 否则,打印Cum_freq[queries[i] – 1]。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum number
// of elements can pick for each query
void findMaxEleCanPick(int arr[], int queries[], int n, int m)
{
mapmp;
for(int i = 0; i < n; i++)
{
if(mp.find(arr[i]) != mp.end())
{
mp[arr[i]] += 1;
}
else
mp[arr[i]] = 1;
}
vectorCum_freq;
// taking out frequencies from map
for(auto i:mp)
Cum_freq.push_back(i.second);
// Sorting in decreasing order
sort(Cum_freq.begin(),Cum_freq.end(),greater());
// Taking cumulative sum
for(int i = 1; i < Cum_freq.size(); i++)
Cum_freq[i] += Cum_freq[i - 1];
// Performing each query
for(int k = 0; k < m; k++)
{
if(queries[k] >= m)
cout << n << " ";
else
cout<
Java
// Java program for the above approach
//Function to find the maximum number
//of elements can pick for each query
import java.io.*;
import java.util.*;
import java.util.HashMap;
class GFG {
static void findMaxEleCanPick(Integer arr[],Integer queries[],Integer n,Integer m)
{
HashMap mp = new HashMap<>();
for(int i=0;i i : mp.entrySet())
Cum_freq[x++]=i.getValue();
//Sorting in decreasing order
Arrays.sort(arr, Collections.reverseOrder());
//Taking cumulative sum
for(Integer i=1;i=m)
System.out.println(n+" ");
else
System.out.println(queries[k]-1);
}
}
// Driver Code
public static void main(String[] args)
{
//Given Input
Integer arr[]={22, 35, 22, 22, 35};
Integer queries[]={ 4, 5, 1 };
// Function Call
findMaxEleCanPick(arr,queries,5,3);
}
}
// java code added by dwivediyash
Python3
# Python program for the above approach
# Function to find the maximum number
# of elements can pick for each query
def findMaxEleCanPick(arr, queries):
# Total elements
N = len(arr)
# Using dictionary
Map = dict()
# Traversing the arr
for ele in arr:
# Updating the dictionary
Map[ele] = 1 + Map.get(ele, 0)
# Taking out frequencies from dictionary
Cum_freq = list(Map.values())
# Sorting in decreasing order
Cum_freq.sort(reverse = True)
# Taking cumulative sum
for i in range(1, len(Cum_freq)):
Cum_freq[i] += Cum_freq[i-1]
# Performing each query
for K in queries:
if K >= len(Cum_freq):
print(N, end = " ")
else:
print(Cum_freq[K-1], end = " ")
# Driver Code
if __name__ == '__main__':
# Given Input
arr = [ 22, 35, 22, 22, 35 ]
queries = [ 4, 5, 1 ]
# Function Call
findMaxEleCanPick(arr, queries)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the maximum number
// of elements can pick for each query
static void findMaxEleCanPick(int []arr, int []queries, int n, int m)
{
Dictionary mp = new Dictionary();
for(int i = 0; i < n; i++)
{
if(mp.ContainsKey(arr[i]))
{
mp[arr[i]] += 1;
}
else
mp.Add(arr[i],1);
}
List Cum_freq = new List();
// taking out frequencies from map
foreach(KeyValuePair entry in mp)
Cum_freq.Add(entry.Value);
// Sorting in decreasing order
Cum_freq.Sort();
Cum_freq.Reverse();
// Taking cumulative sum
for(int i = 1; i < Cum_freq.Count; i++)
Cum_freq[i] += Cum_freq[i - 1];
// Performing each query
for(int k = 0; k < m; k++)
{
if(queries[k] >= m)
Console.Write(n + " ");
else
Console.Write(Cum_freq[queries[k] - 1]);
}
}
// Driver Code
public static void Main()
{
// Given Input
int []arr = {22, 35, 22, 22, 35};
int []queries = { 4, 5, 1 };
// Function Call
findMaxEleCanPick(arr,queries,5,3);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
5 5 3
时间复杂度: O(N)
辅助空间: O(N)