📌  相关文章
📜  长度为K的所有子阵列的最小MEX

📅  最后修改于: 2021-05-31 21:35:30             🧑  作者: Mango

给定由N个不同的正整数和整数K组成的数组arr [] ,任务是从长度为K的所有子数组中找到最小MEX

例子:

天真的方法:解决问题的最简单方法是生成所有长度为K的子数组,并为每个子数组找到MEX 。找到所有MEX后,打印获得的最小的MEX
时间复杂度: O(K * N 2 )
辅助空间: O(1)

高效方法:还可以通过使用“设置和滑动窗口”技术来优化上述方法。请按照以下步骤解决问题:

  • 初始化一个变量,例如mex ,以存储大小为K的所有子数组的MEX中的最小值。
  • 初始化集合S以存储当前子数组中不存在的值。最初在其中插入[1,N + 1]范围内的所有数字,因为最初窗口的大小为0
  • [0,K – 1]范围内进行迭代并从集合中删除元素arr [i]
  • 现在,集合的第一个元素是[0,K]范围内的子数组的MEX ,并将此值存储在mex中
  • 现在,在[K,N – 1]范围内进行迭代并执行以下步骤:
    • 将元素arr [i]插入到集合中。
    • 从集合中删除元素arr [i – K]
    • 现在,集合的第一个元素是当前子数组的MEX 。因此,更新MEX与最小MEX该组的第一个元素的值。
  • 完成上述步骤后,将大小为K的所有子数组中的mex值打印为最小MEX

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to return minimum
// MEX from all K-length subarrays
void minimumMEX(int arr[], int N, int K)
{
    // Stores element from [1, N + 1]
    // which are not present in subarray
    set s;
 
    // Store number 1 to N + 1 in set s
    for (int i = 1; i <= N + 1; i++)
        s.insert(i);
 
    // Find the MEX of K-length
    // subarray starting from index 0
    for (int i = 0; i < K; i++)
        s.erase(arr[i]);
 
    int mex = *(s.begin());
 
    // Find the MEX of all subarrays
    // of length K by erasing arr[i]
    // and inserting arr[i - K]
    for (int i = K; i < N; i++) {
 
        s.erase(arr[i]);
 
        s.insert(arr[i - K]);
 
        // Store first element of set
        int firstElem = *(s.begin());
 
        // Updating the mex
        mex = min(mex, firstElem);
    }
 
    // Print minimum MEX of
    // all K length subarray
    cout << mex << ' ';
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int K = 3;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    minimumMEX(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.HashSet;
 
class GFG{
     
// Function to return minimum
// MEX from all K-length subarrays
static void minimumMEX(int arr[], int N, int K)
{
     
    // Stores element from [1, N + 1]
    // which are not present in subarray
    HashSet s = new HashSet();
 
    // Store number 1 to N + 1 in set s
    for(int i = 1; i <= N + 1; i++)
        s.add(i);
 
    // Find the MEX of K-length
    // subarray starting from index 0
    for(int i = 0; i < K; i++)
        s.remove(arr[i]);
 
    int mex = s.iterator().next();
 
    // Find the MEX of all subarrays
    // of length K by erasing arr[i]
    // and inserting arr[i - K]
    for(int i = K; i < N; i++)
    {
        s.remove(arr[i]);
        s.add(arr[i - K]);
 
        // Store first element of set
        int firstElem = s.iterator().next();
 
        // Updating the mex
        mex = Math.min(mex, firstElem);
    }
 
    // Print minimum MEX of
    // all K length subarray
    System.out.print(mex + " ");
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int K = 3;
    int N = arr.length;
 
    minimumMEX(arr, N, K);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python 3 program for the above approach
 
# Function to return minimum
# MEX from all K-length subarrays
def minimumMEX(arr, N, K):
   
    # Stores element from [1, N + 1]
    # which are not present in subarray
    s = set()
 
    # Store number 1 to N + 1 in set s
    for i in range(1, N + 2, 1):
        s.add(i)
 
    # Find the MEX of K-length
    # subarray starting from index 0
    for i in range(K):
        s.remove(arr[i])
 
    mex = list(s)[0]
 
    # Find the MEX of all subarrays
    # of length K by erasing arr[i]
    # and inserting arr[i - K]
    for i in range(K,N,1):
        s.remove(arr[i])
 
        s.add(arr[i - K])
 
        # Store first element of set
        firstElem = list(s)[0]
 
        # Updating the mex
        mex = min(mex, firstElem)
 
    # Print minimum MEX of
    # all K length subarray
    print(mex)
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6]
    K = 3
    N = len(arr)
    minimumMEX(arr, N, K)
 
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG{
     
// Function to return minimum
// MEX from all K-length subarrays
static void minimumMEX(int[] arr, int N, int K)
{
     
    // Stores element from [1, N + 1]
    // which are not present in subarray
    HashSet s = new HashSet();
 
    // Store number 1 to N + 1 in set s
    for(int i = 1; i <= N + 1; i++)
        s.Add(i);
 
    // Find the MEX of K-length
    // subarray starting from index 0
    for(int i = 0; i < K; i++)
        s.Remove(arr[i]);
    int mex = s.First();
 
    // Find the MEX of all subarrays
    // of length K by erasing arr[i]
    // and inserting arr[i - K]
    for(int i = K; i < N; i++)
    {
        s.Remove(arr[i]);
        s.Add(arr[i - K]);
 
        // Store first element of set
        int firstElem = s.First();
 
        // Updating the mex
        mex = Math.Min(mex, firstElem);
    }
 
    // Print minimum MEX of
    // all K length subarray
    Console.Write(mex + " ");
}
 
// Driver code
static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5, 6 };
    int K = 3;
    int N = arr.Length;
 
    minimumMEX(arr, N, K);
}
}
 
// This code is contributed by abhinavjain194


输出:
1

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。