📌  相关文章
📜  将数组划分为最小数量的具有唯一元素的子数组

📅  最后修改于: 2021-10-27 08:00:25             🧑  作者: Mango

给定一个数组arr 。任务是将数组划分为包含唯一元素的最小数量的子数组,并返回这些子数组的计数。
注意:一个数组元素不能出现在多个子数组中。
例子 :

Input : arr[] = {1, 2, 1, 1, 2, 3}
Output : 3
Explanation : The subarrays having unique elements are 
{ 1, 2 }, { 1 }, and { 1, 2, 3 }

Input : arr[] = {1, 2, 3, 4, 5}
Output : 1
Explanation : The subarray having unique elements is 
{ 1, 2, 3, 4, 5 }

方法:
这个想法是在遍历数组时维护一个集合。在遍历时,如果集合中已经找到一个元素,则将子数组的计数增加 1,因为我们必须将当前元素包含在下一个子数组中并清除新子数组的集合。然后,以自相似的方式处理完整的数组。存储计数的变量将是答案。
下面是上述方法的实现:

C++
// C++ program to count minimum subarray having
// unique elements
#include 
using namespace std;
 
// Function to count minimum number of subarrays
int minimumSubarrays(int ar[], int n)
{
    set se;
 
    int cnt = 1;
 
    for (int i = 0; i < n; i++) {
        // Checking if an element already exist in
        // the current sub-array
        if (se.count(ar[i]) == 0) {
            // inserting the current element
            se.insert(ar[i]);
        }
        else {
            cnt++;
            // clear set for new possible value of subarrays
            se.clear();
            // inserting the current element
            se.insert(ar[i]);
        }
    }
 
    return cnt;
}
 
// Driver Code
int main()
{
    int ar[] = { 1, 2, 1, 3, 4, 2, 4, 4, 4 };
    int n = sizeof(ar) / sizeof(ar[0]);
    cout << minimumSubarrays(ar, n);
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
     
    // Function to count minimum number of subarrays
    static int minimumSubarrays(int ar[], int n)
    {
        Vector se = new Vector();
     
        int cnt = 1;
     
        for (int i = 0; i < n; i++)
        {
             
            // Checking if an element already exist in
            // the current sub-array
            if (se.contains(ar[i]) == false)
            {
                // inserting the current element
                se.add(ar[i]);
            }
            else
            {
                cnt++;
                 
                // clear set for new possible value
                // of subarrays
                se.clear();
                 
                // inserting the current element
                se.add(ar[i]);
            }
        }
        return cnt;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int ar[] = { 1, 2, 1, 3, 4, 2, 4, 4, 4 };
        int n = ar.length ;
         
        System.out.println(minimumSubarrays(ar, n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python 3 implementation of the approach
 
# Function to count minimum number of subarrays
def minimumSubarrays(ar, n) :
    se = []
 
    cnt = 1;
 
    for i in range(n) :
         
        # Checking if an element already exist in
        # the current sub-array
        if se.count(ar[i]) == 0 :
             
            # inserting the current element
            se.append(ar[i])
        else :
            cnt += 1
             
            # clear set for new possible value
            # of subarrays
            se.clear()
             
            # inserting the current element
            se.append(ar[i])
    return cnt
 
# Driver Code
ar = [ 1, 2, 1, 3, 4, 2, 4, 4, 4 ]
n = len(ar)
print(minimumSubarrays(ar, n))
 
# This code is contributed by
# divyamohan123


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;            
 
class GFG
{
     
    // Function to count minimum number of subarrays
    static int minimumSubarrays(int []ar, int n)
    {
        List se = new List();
     
        int cnt = 1;
     
        for (int i = 0; i < n; i++)
        {
             
            // Checking if an element already exist in
            // the current sub-array
            if (se.Contains(ar[i]) == false)
            {
                // inserting the current element
                se.Add(ar[i]);
            }
            else
            {
                cnt++;
                 
                // clear set for new possible value
                // of subarrays
                se.Clear();
                 
                // inserting the current element
                se.Add(ar[i]);
            }
        }
        return cnt;
    }
     
    // Driver Code
    public static void Main(String[] args)
    {
        int []ar = { 1, 2, 1, 3, 4, 2, 4, 4, 4 };
        int n = ar.Length ;
         
        Console.WriteLine(minimumSubarrays(ar, n));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
5

时间复杂度: O(n*log(n))

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程