📌  相关文章
📜  在划分为 [1, N] 个子集后,最小化 Array 中唯一元素的计数总和

📅  最后修改于: 2022-05-13 01:56:05.883000             🧑  作者: Mango

在划分为 [1, N] 个子集后,最小化 Array 中唯一元素的计数总和

给定一个长度为N的数组arr[] ,任务是找到当数组被划分为K个子集(对于范围[1, N]中的所有K )时可能存在的唯一元素的最小总数,即将给定数组划分为K个子集后,每个子集中存在的唯一元素。

例子:

方法:可以根据以下思路解决问题:

请参阅下图以获得更好的理解。

插图:

按照下面提到的步骤来实现上述想法:

  • 使用 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)