在划分为 [1, N] 个子集后,最小化 Array 中唯一元素的计数总和
给定一个长度为N的数组arr[] ,任务是找到当数组被划分为K个子集(对于范围[1, N]中的所有K )时可能存在的唯一元素的最小总数,即将给定数组划分为K个子集后,每个子集中存在的唯一元素。
例子:
Input: arr[] = { 2, 3, 3}
Output: {2, 2, 3}
Explanation:
K = 1: {2, 2, 3}
There are two distinct element in above array = {2, 3}
when array is divided into 1 subarray
K = 2: {2, 2}, { 3 }
There is one type of element in first array and for second subarray,
So the sum of count of unique is 1+1 = 2.
The array can also be divided into 2 subarrays as follows {2, 3}, {2} but then
the count of unique elements will be 2 and 1 and the sum will be 2+1 = 3 which is not the minimum.
K = 3: {2}, {2}, {3}
Unique elements in first subarray = 1
Unique elements in second subarray = 1
Unique elements in third subarray = 1
So total capabilities = 1+1+1 = 3
Input: arr[] = { 3, 1, 2, 2, 2, 4}
Output: {4, 4, 4, 4, 5, 6}
Explanation:
K = 1: {1, 2, 2, 2, 4, 3}
There are 4 type of elements = { 1, 2, 3, 4}
So for k = 1, required sum is = 4
K = 2: {2, 2, 2, 4, 3}, {1}
There are 3 type of elements in first subarray = {2, 3, 4}
So unique elements in this subarray are 3
There is only one type of elements in second subarray = { 1}
So unique elements in this subarray is 1
So for k = 2, total minimum count = 3+1 = 4
K = 3: {2, 2, 2, 3}, {1}, {4}
For k = 3, total minimum count = 2+1+1 = 4
K = 4: {2, 2, 2}, {1}, {4}, {3}
For k = 4, total minimum count = 1+1+1+1 = 4
K = 5: {2, 2}, {1}, {2}, {4}, {3}
For k = 5, total minimum count = 2+1+1+1+1 = 5
K = 6: {1}, {2}, {2}, {2}, {4}, {3}
For k = 6, total minimum count = 1+1+1+1+1+1 = 6
Each subarray contains unique elements.
方法:可以根据以下思路解决问题:
To minimize count of unique elements in each subset, group the elements with same values in one subset.
请参阅下图以获得更好的理解。
插图:
Consider array arr[] = {2, 2, 3}
Total number of unique elements = 2 (2 and 3)
For dividing it in k = 1 parts:
=> There cannot be less than 2 unique elements in total.
=> Divide it into subsets {2, 2, 3}.
=> Total count of unique elements are 2
For dividing it in k = 2 parts:
=> There cannot be less than 2 unique elements in total.
=> Divide it into subsets {2, 2} and {3}.
=> Total count of unique elements are 1 + 1 = 2
For dividing it in k = 3 parts:
=> To minimize total count of unique elements break only one group of similar elements and keep the others intact.
=> Here there was only one group with all similar elements: {2, 2}.
=> Break that into 2 parts: {2}, {2}.
=> Divide it into subsets {2}, {2} and {3}.
=> Total count of unique elements are 1 + 1 + 1 = 3
按照下面提到的步骤来实现上述想法:
- 使用 hashSet 计算不同元素的数量(比如说count )
- 开始迭代 k = 1 到 N
- 如果k最多计数,则尝试将相似元素分组到一个子数组中,以最小化唯一元素的总数。在这种情况下,唯一元素的总数将始终等于count ,因为无法减少唯一元素的数量。
- 如果k大于 count,那么在所有子集中总共会有最少k个唯一元素,因为我们每次都必须打破一组相似的元素,这将增加子数组,因此也会增加唯一元素的总数。
- 打印每个 k 的总数,
下面执行上述方法。
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to count the
// minimum possible number
// of unique elements
void solution(int a[], int n)
{
// To store the unique elements
set hs;
for (int i = 0; i < n; i++)
hs.insert(a[i]);
int cnt = hs.size();
for (int i = 1; i <= n; i++) {
if (i > hs.size()) {
cnt++;
cout << cnt << " ";
}
else {
cout << hs.size() << " ";
}
}
}
// Driver Code
int main()
{
int arr[] = { 2, 3, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
solution(arr, N);
}
// This code is contributed by Taranpreet
Java
// Java implementation of above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to count the
// minimum possible number
// of unique elements
public static void solution(int[] a,
int n)
{
// To store the unique elements
HashSet hs = new HashSet<>();
for (int i : a)
hs.add(i);
int cnt = hs.size();
for (int i = 1; i <= n; i++) {
if (i > hs.size()) {
cnt++;
System.out.print(cnt + " ");
}
else {
System.out.print(hs.size()
+ " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 2, 3, 3 };
int N = arr.length;
solution(arr, N);
}
}
Python
# Python implementation of above approach
# Function to count the
# minimum possible number
# of unique elements
def solution(a, n):
# To store the unique elements
hs = set()
for i in range(0, n):
hs.add(a[i])
cnt = len(hs)
for i in range(1, n + 1):
if (i > len(hs)):
cnt += 1
print(cnt)
else:
print(len(hs))
# Driver Code
arr = [2, 3, 3]
N = len(arr)
solution(arr, N)
# This code is contributed by Samim Hossain Mondal.
C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to count the
// minimum possible number
// of unique elements
static void solution(int[] a,
int n)
{
// To store the unique elements
HashSet hs = new HashSet();
foreach(int i in a)
{
hs.Add(i);
}
int cnt = hs.Count;
for (int i = 1; i <= n; i++) {
if (i > hs.Count) {
cnt++;
Console.Write(cnt + " ");
}
else {
Console.Write(hs.Count
+ " ");
}
}
}
// Driver Code
static public void Main (){
int[] arr = { 2, 3, 3 };
int N = arr.Length;
solution(arr, N);
}
}
// This code is contributed by hrithikgarg03188.
Javascript
2 2 3
时间复杂度: O(N)
辅助空间: O(N)