给定一个由N 个整数组成的数组arr[] ,任务是计算最多需要删除K 个相等元素以使数组为空的最少次数。
例子:
Input: arr[] = {1, 3, 1, 1, 3}, K = 2
Output: 3
Explanation:
Step 1: Remove at most 2 1s from the array. The modified array is {1, 3, 3}.
Step 2: Remove at most 2 3s from the array. The modified array is {1}.
Step 3: Remove at most 2 1s from the array. The modified array is {}.
After 3 steps, the array becomes empty.
Therefore, the minimum number of steps required is 3.
Input: arr[] = {4, 4, 7, 3, 1, 1, 2, 1, 7, 3}, K = 5
Output: 5
朴素的方法:最简单的方法是遍历数组并计算每个数组元素的频率,然后将每个元素的频率除以K并将其添加到count 。如果数组元素的频率不能被K整除,则递增计数。完成以上步骤后,打印count的值作为结果。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:上述方法可以通过Hashing进行优化,存储每个数组元素的频率,然后计算所需的最小操作次数。请按照以下步骤解决问题:
- 初始化一个变量,比如count ,它存储所需的最少步骤数。
- 初始化存储数组中每个元素的频率的 Hashmap。
- 遍历数组arr[]并将每个元素的频率存储在 Hashmap 中。
- 遍历 Hashmap 并将每个元素的频率值除以K后添加到变量count 中。如果当前数组元素的频率不能被K整除,则将计数增加1 。
- 完成上述步骤后,打印count作为使数组为空所需的最小步骤数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the minimum
// number of steps required to empty
// given array by removing at most K
// equal array elements in each operation
void minSteps(int arr[], int N, int K)
{
// Stores the minimum number of
// steps required to empty the array
int count = 0;
// Stores the occurrence
// of each array element
map cntFreq;
for (int i = 0; i < N; i++)
{
// Update the frequency
cntFreq[arr[i]]++;
}
// Traverse the Hashmap
for (auto i : cntFreq)
{
// Check if the frequency
// is divisible by K or not
if (i.first % K == 0)
count += i.second / K;
// Otherwise
else
count += (i.second / K) + 1;
}
// Print the count of
// minimum steps required
cout << (count);
}
// Driver Code
int main()
{
int arr[] = { 4, 4, 7, 3, 1,
1, 2, 1, 7, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 5;
minSteps(arr, N, K);
return 0;
}
// This code is contributed by Dharanendra L V.
Java
// Java program for the above approach
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class GFG {
// Function to count the minimum
// number of steps required to empty
// given array by removing at most K
// equal array elements in each operation
public static void minSteps(
int[] arr, int N, int K)
{
// Stores the minimum number of
// steps required to empty the array
int count = 0;
// Stores the occurrence
// of each array element
Map cntFreq
= new HashMap();
for (int i = 0; i < N; i++) {
// Update the frequency
cntFreq.put(
arr[i],
cntFreq.getOrDefault(
arr[i], 0)
+ 1);
}
// Traverse the Hashmap
for (Integer i : cntFreq.keySet()) {
// Check if the frequency
// is divisible by K or not
if (cntFreq.get(i) % K == 0)
count += cntFreq.get(i)
/ K;
// Otherwise
else
count += (cntFreq.get(i)
/ K)
+ 1;
}
// Print the count of
// minimum steps required
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 4, 7, 3, 1,
1, 2, 1, 7, 3 };
int N = arr.length;
int K = 5;
minSteps(arr, N, K);
}
}
Python3
# Python3 program for the above approach
# Function to count the minimum
# number of steps required to empty
# given array by removing at most K
# equal array elements in each operation
def minSteps(arr, N, K) :
# Stores the minimum number of
# steps required to empty the array
count = 0
# Stores the occurrence
# of each array element
cntFreq = {}
for i in range(N) :
# Update the frequency
if arr[i] in cntFreq :
cntFreq[arr[i]] += 1
else :
cntFreq[arr[i]] = 1
# Traverse the Hashmap
for i in cntFreq :
# Check if the frequency
# is divisible by K or not
if (i % K == 0) :
count += cntFreq[i] // K
# Otherwise
else :
count += (cntFreq[i] // K) + 1
# Print the count of
# minimum steps required
print(count)
arr = [ 4, 4, 7, 3, 1, 1, 2, 1, 7, 3 ]
N = len(arr)
K = 5
minSteps(arr, N, K)
# This code is contributed by divyeshabadiya07.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to count the minimum
// number of steps required to empty
// given array by removing at most K
// equal array elements in each operation
public static void minSteps(
int[] arr, int N, int K)
{
// Stores the minimum number of
// steps required to empty the array
int count = 0;
// Stores the occurrence
// of each array element
Dictionary cntFreq
= new Dictionary();
for (int i = 0; i < N; i++) {
// Update the frequency
if(cntFreq.ContainsKey(arr[i]))
cntFreq[arr[i]] = cntFreq[arr[i]]+1;
else
cntFreq.Add(arr[i],1);
}
// Traverse the Hashmap
foreach (int i in cntFreq.Keys) {
// Check if the frequency
// is divisible by K or not
if (cntFreq[i] % K == 0)
count += cntFreq[i]
/ K;
// Otherwise
else
count += (cntFreq[i]
/ K)
+ 1;
}
// Print the count of
// minimum steps required
Console.Write(count);
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 4, 4, 7, 3, 1,
1, 2, 1, 7, 3 };
int N = arr.Length;
int K = 5;
minSteps(arr, N, K);
}
}
// This code is contributed by shikhasingrajput
Javascript
5
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。