给定一个整数数组,在删除给定元素后找到 k 个最小的数字。在重复元素的情况下,对于包含要删除的元素的数组中存在的每个元素实例,仅删除给定数组中的一个实例。
假设在进行 n 次删除后,数组中至少还剩下 k 个元素。
例子:
Input : array[] = { 5, 12, 33, 4, 56, 12, 20 }, del[] = { 12, 56, 5 }, k = 3
Output : 4 12 20
Explanation : After deletions { 33, 4, 12, 20 } will be left. Print top 3 smallest elements from it.
方法 :
- 将哈希映射中所有要从数组中删除的数字插入,以便我们可以在 O(1) 时间内检查数组中的元素是否也存在于 Delete-array 中。
- 遍历数组。检查该元素是否存在于哈希映射中。
- 如果存在,将其从哈希映射中删除。
- 否则,将其插入到 Min 堆中。
- 插入除要删除的元素之外的所有元素后,从Min堆中弹出k个元素。
C++
#include "iostream"
#include "queue"
#include "unordered_map"
#include "vector"
using namespace std;
// Find k minimum element from arr[0..m-1] after deleting
// elements from del[0..n-1]
void findElementsAfterDel(int arr[], int m, int del[],
int n, int k)
{
// Hash Map of the numbers to be deleted
unordered_map mp;
for (int i = 0; i < n; ++i) {
// Increment the count of del[i]
mp[del[i]]++;
}
priority_queue, greater > heap;
for (int i = 0; i < m; ++i) {
// Search if the element is present
if (mp.find(arr[i]) != mp.end()) {
// Decrement its frequency
mp[arr[i]]--;
// If the frequency becomes 0,
// erase it from the map
if (mp[arr[i]] == 0)
mp.erase(arr[i]);
}
// Else push it in the min heap
else
heap.push(arr[i]);
}
// Print top k elements in the min heap
for (int i = 0; i < k; ++i) {
cout << heap.top() << " ";
// Pop the top element
heap.pop();
}
}
int main()
{
int array[] = { 5, 12, 33, 4, 56, 12, 20 };
int m = sizeof(array) / sizeof(array[0]);
int del[] = { 12, 56, 5 };
int n = sizeof(del) / sizeof(del[0]);
int k = 3;
findElementsAfterDel(array, m, del, n, k);
return 0;
}
Java
// Java program to find the k maximum
// number from the array after n deletions
import java.util.*;
public class GFG
{
// Find k minimum element from arr[0..m-1] after deleting
// elements from del[0..n-1]
static void findElementsAfterDel(int[] arr,
int m, int[] del,
int n, int k)
{
// Hash Map of the numbers to be deleted
HashMap mp = new HashMap<>();
for (int i = 0; i < n; ++i)
{
// Increment the count of del[i]
if(mp.containsKey(del[i]))
{
mp.put(del[i], mp.get(del[i])+1);
}
else{
mp.put(del[i], 1);
}
}
Vector heap = new Vector();
for (int i = 0; i < m; ++i)
{
// Search if the element is present
if (mp.containsKey(arr[i]))
{
// Decrement its frequency
mp.put(arr[i], mp.get(arr[i]) - 1);
// If the frequency becomes 0,
// erase it from the map
if (mp.get(arr[i]) == 0)
mp.remove(arr[i]);
}
// Else push it in the min heap
else
heap.add(arr[i]);
}
Collections.sort(heap);
// Print top k elements in the min heap
for (int i = 0; i < k; ++i)
{
System.out.print(heap.get(0) + " ");
// Pop the top element
heap.remove(0);
}
}
// Driver code
public static void main(String[] args)
{
int[] array = { 5, 12, 33, 4, 56, 12, 20 };
int m = array.length;
int[] del = { 12, 56, 5 };
int n = del.length;
int k = 3;
findElementsAfterDel(array, m, del, n, k);
}
}
// This code is contributed by divvyeshrabadiya07.
Python3
# Python3 program to find the k maximum
# number from the array after n deletions
import math as mt
# Find k maximum element from arr[0..m-1]
# after deleting elements from del[0..n-1]
def findElementsAfterDel(arr, m, dell, n, k):
# Hash Map of the numbers to be deleted
mp = dict()
for i in range(n):
# Increment the count of del[i]
if dell[i] in mp.keys():
mp[dell[i]] += 1
else:
mp[dell[i]] = 1
heap = list()
for i in range(m):
# Search if the element is present
if (arr[i] in mp.keys()):
# Decrement its frequency
mp[arr[i]] -= 1
# If the frequency becomes 0,
# erase it from the map
if (mp[arr[i]] == 0):
mp.pop(arr[i])
# Else push it
else:
heap.append(arr[i])
heap.sort()
return heap[:k]
# Driver code
array = [5, 12, 33, 4, 56, 12, 20]
m = len(array)
dell = [12, 56, 5 ]
n = len(dell)
k = 3
print(*findElementsAfterDel(array, m, dell, n, k))
# This code is contributed
# by mohit kumar 29
C#
// C# program to find the k maximum
// number from the array after n deletions
using System;
using System.Collections.Generic;
class GFG
{
// Find k minimum element from arr[0..m-1] after deleting
// elements from del[0..n-1]
static void findElementsAfterDel(int[] arr, int m, int[] del,
int n, int k)
{
// Hash Map of the numbers to be deleted
Dictionary mp = new Dictionary();
for (int i = 0; i < n; ++i)
{
// Increment the count of del[i]
if(mp.ContainsKey(del[i]))
{
mp[del[i]]++;
}
else{
mp[del[i]] = 1;
}
}
List heap = new List();
for (int i = 0; i < m; ++i)
{
// Search if the element is present
if (mp.ContainsKey(arr[i]))
{
// Decrement its frequency
mp[arr[i]]--;
// If the frequency becomes 0,
// erase it from the map
if (mp[arr[i]] == 0)
mp.Remove(arr[i]);
}
// Else push it in the min heap
else
heap.Add(arr[i]);
}
heap.Sort();
// Print top k elements in the min heap
for (int i = 0; i < k; ++i)
{
Console.Write(heap[0] + " ");
// Pop the top element
heap.RemoveAt(0);
}
}
// Driver code
static void Main()
{
int[] array = { 5, 12, 33, 4, 56, 12, 20 };
int m = array.Length;
int[] del = { 12, 56, 5 };
int n = del.Length;
int k = 3;
findElementsAfterDel(array, m, del, n, k);
}
}
// This code is contributed by divyesh072019.
Javascript
输出:
4 12 20