通过删除所有出现的任何数字来最小化 Array 中的删除,从而使数组大小减少到至少一半
给定一个正整数数组arr[] ,任务是从数组中选择一个元素并删除所有出现的元素,以使所选元素的数量最少,并且数组大小至少变为其原始大小的一半。
注意:给定数组的大小总是偶数。
例子:
Input: arr[] = {2, 2, 2, 2, 4, 4, 6, 7, 7, 3, 3, 3}
Output: 2
Explanation: First we select 2 and delete all its occurrences after that arr[] becomes – {4, 4, 6, 7, 7, 3, 3, 3} with size = 8. As the size is still greater than half, we select 3 and delete all its occurrences, after that arr[] becomes – {4, 4, 6, 7, 7} with size = 5.
Input: arr[] = {3, 3, 3, 3, 3}
Output: 1
Explanation : select 3 and remove all its occurrences.
Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 4
Explanation : Since there is no duplicate elements, hence any 4 elements can be removed to make the array length at least half.
方法:通过删除频率最高的元素可以轻松完成该任务,只要数组大小变为实际大小的至少一半,我们就会返回到目前为止删除的唯一元素的数量。
请按照以下步骤解决问题:
- 使用 Hash-map 存储数组中存在的元素的频率。
- 将频率存储在列表中。
- 对列表进行排序并从后面遍历它。
- 选择最大的频率元素并从数组大小中减少它并增加删除的唯一元素的计数。
- 如果新数组大小变为原始数组大小的至少一半,则返回到现在为止的唯一元素数。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to calculate the minimum
// elements removed
int reduceArrSize(int arr[],int n)
{
unordered_map hm;
// Making frequency map of elements
for (int i = 0; i < n; i++) {
hm[arr[i]]++;
}
// Storing frequencies in a list
vector freq;
for(auto it = hm.begin(); it != hm.end(); it++)
{
freq.push_back(it->second);
}
// Sorting the list
sort(freq.begin(), freq.end());
int size = n;
int idx = freq.size() - 1;
int count = 0;
// Counting number of elements to be deleted
while (size > n/ 2) {
size -= freq[idx--];
count++;
}
return count;
}
// Driver Code
int main()
{
int arr[] = { 2, 2, 2, 2, 4, 4,
6, 7, 7, 3, 3, 3 };
int n = sizeof(arr)/sizeof(arr[0]);
int count = reduceArrSize(arr, n);
cout<<(count);
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to calculate the minimum
// elements removed
public static int reduceArrSize(int[] arr)
{
HashMap hm = new HashMap<>();
// Making frequency map of elements
for (int i = 0; i < arr.length; i++) {
hm.put(arr[i], hm.getOrDefault(arr[i], 0) + 1);
}
// Storing frequencies in a list
ArrayList freq
= new ArrayList(hm.values());
// Sorting the list
Collections.sort(freq);
int size = arr.length;
int idx = freq.size() - 1;
int count = 0;
// Counting number of elements to be deleted
while (size > arr.length / 2) {
size -= freq[idx--];
count++;
}
return count;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 2, 2, 2, 4, 4,
6, 7, 7, 3, 3, 3 };
int count = reduceArrSize(arr);
System.out.println(count);
}
}
Python3
# python 3 Program to implement
# the above approach
# Function to calculate the minimum
# elements removed
def reduceArrSize(arr,n):
hm = {}
# Making frequency map of elements
for i in range(n):
if arr[i] in hm:
hm[arr[i]] += 1
else:
hm[arr[i]] = 1
# Storing frequencies in a list
freq = []
for key,value in hm.items():
freq.append(value)
# Sorting the list
freq.sort()
size = n
idx = len(freq) - 1
count = 0
# Counting number of elements to be deleted
while (size > n// 2):
size -= freq[idx]
idx -= 1
count += 1
return count
# Driver Code
if __name__ == '__main__':
arr = [2, 2, 2, 2, 4, 4,6, 7, 7, 3, 3, 3]
n = len(arr)
count = reduceArrSize(arr, n)
print(count)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to calculate the minimum
// elements removed
public static int reduceArrSize(int[] arr)
{
Dictionary hm = new Dictionary();
// Making frequency map of elements
for (int i = 0; i < arr.Length; i++) {
if(hm.ContainsKey(arr[i])){
hm[arr[i]] = hm[arr[i]]+1;
}
else{
hm.Add(arr[i], 1);
}
}
// Storing frequencies in a list
List freq
= new List(hm.Values);
// Sorting the list
freq.Sort();
int size = arr.Length;
int idx = freq.Count - 1;
int count = 0;
// Counting number of elements to be deleted
while (size > arr.Length / 2) {
size -= freq[idx--];
count++;
}
return count;
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 2, 2, 2, 2, 4, 4,
6, 7, 7, 3, 3, 3 };
int count = reduceArrSize(arr);
Console.WriteLine(count);
}
}
// This code is contributed by 29AjayKumar
Javascript
2
时间复杂度: O(N*logN),排序频率列表
辅助空间: O(N),hashmap 来存储频率