📜  给定大小的子数组中最大唯一整数数

📅  最后修改于: 2021-04-29 05:02:11             🧑  作者: Mango

给定一个由N个整数和一个数M组成的数组。任务是在大小为M的所有可能的连续子数组中找出最大的唯一整数数。
例子

天真的方法

  1. 生成大小为M的所有子数组。
  2. 计算每个子数组的唯一编号。
    • 检查其是否大于先前的最大唯一编号,如果是,则将其替换为先前的最大唯一编号。
  3. 继续直到我们生成所有可能的子数组。

下面是上述方法的实现:

C++
// A C++ programme to find maximum distinct elements
// in a subarray of size k
#include
using namespace std;
//Function to find maximum unique element in
//a subarray of size k
int maxUniqueNum(int a[],int N,int M)
{
    int maxUnique=0;
    //search every subarray of size k
    //and find how many unique element present
    for(int i=0;i s;
        for(int j=0;jmaxUnique)
        {
            maxUnique=s.size();
        }
    }
    return maxUnique;
}
     
int main()
{
    int arr[] = {5, 3, 5, 2, 3, 2};
    int M=3,N=sizeof(arr)/sizeof(arr[0]);
    cout<


Java
// Java Program to find maximum number of
// Unique integers in Sub-Array
// of given size
 
import java.util.*;
class GFG {
 
    // Function to find maximum number of
    // Unique integers in Sub-Array
    // of given size
    public static int maxUniqueNum(int arr[],
                                   int N, int M)
    {
        int maxUnique = 0;
 
        // Generate all subarrays of size M
        for (int i = 0; i <= N - M; i++) {
            int currentUnique = 0;
 
            HashMap map = new HashMap();
 
            for (int k = i; k < i + M; k++) {
 
                // if the key is new to the map,
                // push the key in map and increment
                // count for unique number
                if (!map.containsKey(arr[k])) {
                    map.put(arr[i], 1);
                    currentUnique++;
                }
            }
 
            if (currentUnique > maxUnique)
                maxUnique = currentUnique;
        }
 
        return maxUnique;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 5, 3, 5, 2, 3, 2 };
        int N = 6;
 
        int M = 3;
 
        System.out.println(maxUniqueNum(arr, N, M));
    }
}


Python3
# A python3 programme to find maximum
# distinct elements in a subarray of size k
 
# Function to find maximum unique
# element in a subarray of size k
def maxUniqueNum(a, N, M):
    maxUnique = 0
     
    # search every subarray of size k and
    # find how many unique element present
    for i in range(N - M):
         
        # create an empty set to store
        # the unique elements
        s = set()
 
        for j in range(M):
            # insert all elements
            # duplicate elements are not
            # stored in set
            s.add(a[i + j])
         
        # update the maxUnique
        if(len(s) > maxUnique):
            maxUnique = len(s)
     
    return maxUnique
 
# Driver Code   
if __name__ == '__main__':
    arr = [5, 3, 5, 2, 3, 2]
    M = 3
    N = len(arr)
    print(maxUniqueNum(arr, N, M))
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# Program to find maximum number of
// Unique integers in Sub-Array
// of given size
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to find maximum number of
    // Unique integers in Sub-Array
    // of given size
    public static int maxUniqueNum(int []arr,
                                int N, int M)
    {
        int maxUnique = 0;
        // Generate all subarrays of size M
        for (int i = 0; i < N - M; i++)
        {
            int currentUnique = 0;
 
            Dictionary map = new Dictionary();
            for (int k = i; k < i + M; k++)
            {
 
                // if the key is new to the map,
                // push the key in map and increment
                // count for unique number
                if (!map.ContainsKey(arr[k]))
                {
                    map.Remove(arr[i]);
                    map.Add(arr[i], 1);
                    currentUnique++;
                    continue;
                }
            }
 
            if (currentUnique > maxUnique)
                maxUnique = currentUnique;
        }
 
        return maxUnique;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arr = { 5, 3, 5, 2, 3, 2 };
        int N = 6;
        int M = 3;
        Console.WriteLine(maxUniqueNum(arr, N, M));
    }
}
 
// This code has been contributed by 29AjayKumar


C++
// An efficient Approach to count distinct elements in
// every window of size k
#include
using namespace std;
//Function to find maximum unique element in
//a subarray of size k
int max_U_element(int a[],int N,int M)
{
    //map to store the unique elements and their size
    map hash;
    //Number of unique elements in an window
    int dist_count=0;    
    int res=0;     //Maximum unique element in a window
    //store all elements till size k i.e.
    //storing first window
    for(int i=0;i


Java
// An efficient Java program to count distinct elements in
// every window of size k
import java.util.HashMap;
 
class maxUniqueNumWindow {
    static int maxUniqueNum(int arr[], int M)
    {
        // Creates an empty hashMap hM
        HashMap hM = new HashMap();
 
        // initialize distinct element count for
        // current window
        int dist_count = 0;
 
        // Traverse the first window and store count
        // of every element in hash map
        for (int i = 0; i < M; i++) {
            if (hM.get(arr[i]) == null) {
                hM.put(arr[i], 1);
                dist_count++;
            }
            else {
                int count = hM.get(arr[i]);
                hM.put(arr[i], count + 1);
            }
        }
  
        int res = dist_count;
 
        // Traverse through the remaining array
        for (int i = M; i < arr.length; i++) {
 
            // Remove first element of previous window
            // If there was only one occurrence, then
            // reduce distinct count.
            if (hM.get(arr[i - M]) == 1) {
                hM.remove(arr[i - M]);
                dist_count--;
            }
 
            else // reduce count of the removed element
            {
                int count = hM.get(arr[i - M]);
                hM.put(arr[i - M], count - 1);
            }
 
            // Add new element of current window
            // If this element appears first time,
            // increment distinct element count
            if (hM.get(arr[i]) == null) {
                hM.put(arr[i], 1);
                dist_count++;
            }
            else // Increment distinct element count
            {
                int count = hM.get(arr[i]);
                hM.put(arr[i], count + 1);
            }
 
            res = Math.max(res, dist_count);
        }
 
        return res;
    }
 
    // Driver method
    public static void main(String arg[])
    {
        int arr[] = { 1, 2, 1, 3, 4, 2, 3 };
        int M = 4;
        System.out.println(maxUniqueNum(arr, M));
    }
}


Python3
# An efficient Approach to count distinct elements in
# every window of size k
# Function to find maximum unique element in
# a subarray of size k
def max_U_element(a, N, M):
     
    # map to store the unique elements and their size
    hsh = dict()
     
    # Number of unique elements in an window
    dist_count = 0
    res = 0
     
    # Maximum unique element in a window
    # store all elements till size k i.e.
    # storing first window
    for i in range(M):
         
        # found an unique element
        if(arr[i] not in hsh.keys()):
            hsh[a[i]] = 1
            dist_count += 1
             
        # an Duplicate element inserting
        else:
             
            # Update the size of that element
            hsh[a[i]] += 1
 
    res = dist_count
    # Traverse till the end of array
    for i in range(M, N):
         
        # Remove fist element from map
        if(a[i - M] in hsh.keys() and hsh[a[i - M]] == 1):
         
            # when element present only one time
            # in window so delete this
            del hsh[a[i-M]]
            dist_count -= 1
        else:
            # when multiple time element has occurred
            # in window so decrease size by one
            hsh[a[i - M]] -= 1
             
        # Add new element to map
        # If element is unique to map
        # increment count
        if(a[i] not in hsh.keys()):
            hsh[a[i]] = 1
            dist_count += 1
             
        # Duplicate element found
        # update the size of that element
        else:
            hsh[a[i]] += 1
             
        # Update the res
        res = max(res, dist_count)
 
    return res
 
# Driver code
arr = [1, 2, 1, 3, 4, 2, 3]
M = 4
N = len(arr)
print(max_U_element(arr, N, M))
 
# This code is contributed by mohit kumar


C#
// An efficient C# program to
// count distinct elements in
// every window of size k
using System;
using System.Collections.Generic;
 
class GFG
{
    static int maxUniqueNum(int []arr, int M)
    {
        // Creates an empty hashMap hM
        Dictionary hM = new Dictionary();
 
        // initialize distinct element count
        // for current window
        int dist_count = 0;
 
        // Traverse the first window and store
        // count of every element in hash map
        for (int i = 0; i < M; i++)
        {
            if (!hM.ContainsKey(arr[i]))
            {
                hM.Add(arr[i], 1);
                dist_count++;
            }
            else
            {
                int count = hM[arr[i]];
                hM[arr[i]] = count + 1;
            }
        }
 
        int res = dist_count;
 
        // Traverse through the remaining array
        for (int i = M; i < arr.Length; i++)
        {
 
            // Remove first element of previous window
            // If there was only one occurrence, then
            // reduce distinct count.
            if (hM[arr[i - M]] == 1)
            {
                hM.Remove(arr[i - M]);
                dist_count--;
            }
 
            // reduce count of the removed element
            else
            {
                int count = hM[arr[i - M]];
                hM[arr[i - M]] = count - 1;
            }
 
            // Add new element of current window
            // If this element appears first time,
            // increment distinct element count
            if (!hM.ContainsKey(arr[i]))
            {
                hM.Add(arr[i], 1);
                dist_count++;
            }
             
            // Increment distinct element count
            else
            {
                int count = hM[arr[i]];
                hM[arr[i]] = count + 1;
            }
            res = Math.Max(res, dist_count);
        }
        return res;
    }
 
    // Driver Code
    public static void Main(String []arg)
    {
        int []arr = { 1, 2, 1, 3, 4, 2, 3 };
        int M = 4;
        Console.WriteLine(maxUniqueNum(arr, M));
    }
}
 
// This code is contributed by 29AjayKumar


输出:
3

时间复杂度: O(M * N)
辅助空间: O(M)

高效的解决方案高效的解决方案是使用窗口滑动技术。我们维护一个哈希表,用于存储每个窗口的唯一元素。
1)在哈希图中存储前M个元素的计数。
2)从第(M + 1)个元素开始遍历,对于每个元素,将其添加到哈希图中,并删除上一个窗口的第一个元素。

下面是上述方法的实现:

C++

// An efficient Approach to count distinct elements in
// every window of size k
#include
using namespace std;
//Function to find maximum unique element in
//a subarray of size k
int max_U_element(int a[],int N,int M)
{
    //map to store the unique elements and their size
    map hash;
    //Number of unique elements in an window
    int dist_count=0;    
    int res=0;     //Maximum unique element in a window
    //store all elements till size k i.e.
    //storing first window
    for(int i=0;i

Java

// An efficient Java program to count distinct elements in
// every window of size k
import java.util.HashMap;
 
class maxUniqueNumWindow {
    static int maxUniqueNum(int arr[], int M)
    {
        // Creates an empty hashMap hM
        HashMap hM = new HashMap();
 
        // initialize distinct element count for
        // current window
        int dist_count = 0;
 
        // Traverse the first window and store count
        // of every element in hash map
        for (int i = 0; i < M; i++) {
            if (hM.get(arr[i]) == null) {
                hM.put(arr[i], 1);
                dist_count++;
            }
            else {
                int count = hM.get(arr[i]);
                hM.put(arr[i], count + 1);
            }
        }
  
        int res = dist_count;
 
        // Traverse through the remaining array
        for (int i = M; i < arr.length; i++) {
 
            // Remove first element of previous window
            // If there was only one occurrence, then
            // reduce distinct count.
            if (hM.get(arr[i - M]) == 1) {
                hM.remove(arr[i - M]);
                dist_count--;
            }
 
            else // reduce count of the removed element
            {
                int count = hM.get(arr[i - M]);
                hM.put(arr[i - M], count - 1);
            }
 
            // Add new element of current window
            // If this element appears first time,
            // increment distinct element count
            if (hM.get(arr[i]) == null) {
                hM.put(arr[i], 1);
                dist_count++;
            }
            else // Increment distinct element count
            {
                int count = hM.get(arr[i]);
                hM.put(arr[i], count + 1);
            }
 
            res = Math.max(res, dist_count);
        }
 
        return res;
    }
 
    // Driver method
    public static void main(String arg[])
    {
        int arr[] = { 1, 2, 1, 3, 4, 2, 3 };
        int M = 4;
        System.out.println(maxUniqueNum(arr, M));
    }
}

Python3

# An efficient Approach to count distinct elements in
# every window of size k
# Function to find maximum unique element in
# a subarray of size k
def max_U_element(a, N, M):
     
    # map to store the unique elements and their size
    hsh = dict()
     
    # Number of unique elements in an window
    dist_count = 0
    res = 0
     
    # Maximum unique element in a window
    # store all elements till size k i.e.
    # storing first window
    for i in range(M):
         
        # found an unique element
        if(arr[i] not in hsh.keys()):
            hsh[a[i]] = 1
            dist_count += 1
             
        # an Duplicate element inserting
        else:
             
            # Update the size of that element
            hsh[a[i]] += 1
 
    res = dist_count
    # Traverse till the end of array
    for i in range(M, N):
         
        # Remove fist element from map
        if(a[i - M] in hsh.keys() and hsh[a[i - M]] == 1):
         
            # when element present only one time
            # in window so delete this
            del hsh[a[i-M]]
            dist_count -= 1
        else:
            # when multiple time element has occurred
            # in window so decrease size by one
            hsh[a[i - M]] -= 1
             
        # Add new element to map
        # If element is unique to map
        # increment count
        if(a[i] not in hsh.keys()):
            hsh[a[i]] = 1
            dist_count += 1
             
        # Duplicate element found
        # update the size of that element
        else:
            hsh[a[i]] += 1
             
        # Update the res
        res = max(res, dist_count)
 
    return res
 
# Driver code
arr = [1, 2, 1, 3, 4, 2, 3]
M = 4
N = len(arr)
print(max_U_element(arr, N, M))
 
# This code is contributed by mohit kumar

C#

// An efficient C# program to
// count distinct elements in
// every window of size k
using System;
using System.Collections.Generic;
 
class GFG
{
    static int maxUniqueNum(int []arr, int M)
    {
        // Creates an empty hashMap hM
        Dictionary hM = new Dictionary();
 
        // initialize distinct element count
        // for current window
        int dist_count = 0;
 
        // Traverse the first window and store
        // count of every element in hash map
        for (int i = 0; i < M; i++)
        {
            if (!hM.ContainsKey(arr[i]))
            {
                hM.Add(arr[i], 1);
                dist_count++;
            }
            else
            {
                int count = hM[arr[i]];
                hM[arr[i]] = count + 1;
            }
        }
 
        int res = dist_count;
 
        // Traverse through the remaining array
        for (int i = M; i < arr.Length; i++)
        {
 
            // Remove first element of previous window
            // If there was only one occurrence, then
            // reduce distinct count.
            if (hM[arr[i - M]] == 1)
            {
                hM.Remove(arr[i - M]);
                dist_count--;
            }
 
            // reduce count of the removed element
            else
            {
                int count = hM[arr[i - M]];
                hM[arr[i - M]] = count - 1;
            }
 
            // Add new element of current window
            // If this element appears first time,
            // increment distinct element count
            if (!hM.ContainsKey(arr[i]))
            {
                hM.Add(arr[i], 1);
                dist_count++;
            }
             
            // Increment distinct element count
            else
            {
                int count = hM[arr[i]];
                hM[arr[i]] = count + 1;
            }
            res = Math.Max(res, dist_count);
        }
        return res;
    }
 
    // Driver Code
    public static void Main(String []arg)
    {
        int []arr = { 1, 2, 1, 3, 4, 2, 3 };
        int M = 4;
        Console.WriteLine(maxUniqueNum(arr, M));
    }
}
 
// This code is contributed by 29AjayKumar
输出:
4

时间复杂度: O(N)
辅助空间: O(M)