📌  相关文章
📜  找到包含所有元素的最小子数组以相同的顺序

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

找到包含所有元素的最小子数组以相同的顺序

给定两个大小为 m 和 n 的整数数组。任务是在包含第二个数组的所有元素的第一个数组中找到最小长度的子数组。
注意:第二个数组的元素可能以非连续的方式出现在大数组中,但顺序必须相同。 (m < n)
例子 :

Input :  A[] = {2, 2, 4, 5, 8, 9}  
         B[] = {2, 5, 9}  
Output : 5
Smallest subarray of A[] that contains all
elements of B[] is {2, 4, 5, 8, 9} which is
of size 5.

Input :  A[] = {5, 6, 5, 2, 7, 5, 6, 7, 5, 5, 7}  
         B[] = {5, 5, 7}  
Output : 3

方法1(天真)

一个简单的解决方案是生成给定数组的所有子数组,并检查每个子数组是否包含另一个数组的元素。最后,返回包含另一个数组的子数组的最小长度。
下面是上述想法的实现。

C++
// CPP program to find smallest length
// subarray that contains all elements
// of another array.
#include 
using namespace std;
 
// function return the minimum length of sub_array
int minimumSubArray(int A[], int n, int B[], int m)
{
    int result = INT_MAX;
 
    // Pick starting point
    for (int i = 0; i < n; i++) {
 
        // Pick ending point
        for (int j = i; j < n; j++) {
 
            // k is index in first array and
            // 'index' is index in second array.
            int k, index = 0;
            for (k = i; k <= j; k++) {
                if (A[k] == B[index])
                    index++;
                if (index == m)
                    break;
            }
 
            // update minimum length sub_array
            if (index == m && result > k - i + 1)
              result = (k == n) ? k - i : k - i + 1;
        }
    }
 
    // return minimum length subarray
    return result;
}
 
// driver program to test above function
int main()
{
    int A[] = { 5, 6, 5, 2, 7, 5, 6, 7, 5, 5, 7 };
    int B[] = { 5, 5, 7 };
    int n = sizeof(A)/sizeof(A[0]);
    int m = sizeof(B)/sizeof(B[0]);
    cout << minimumSubArray(A, n, B, m);
    return 0;
}


Java
// Java program to find smallest length
// subarray that contains all elements
// of another array.
import java.io.*;
 
class GFG {
 
// function return the minimum length
// of sub_array
static int minimumSubArray(int A[], int n,
                           int B[], int m)
{
    int result = Integer.MAX_VALUE;
 
    // Pick starting point
    for (int i = 0; i < n; i++) {
 
        // Pick ending point
        for (int j = i; j < n; j++) {
 
            // k is index in first array
            // and 'index' is index in
            // second array.
            int k, index = 0;
            for (k = i; k <= j; k++) {
                if (A[k] == B[index])
                    index++;
                if (index == m)
                    break;
            }
 
            // update minimum length sub_array
            if (index == m && result > k - i + 1)
            result = (k == n) ? k - i : k - i + 1;
        }
    }
 
    // return minimum length subarray
    return result;
}
 
// driver program to test above function
public static void main(String[] args)
{
    int A[] = { 5, 6, 5, 2, 7, 5,
                6, 7, 5, 5, 7 };
    int B[] = { 5, 5, 7 };
    int n = A.length;
    int m = B.length;
    System.out.println(minimumSubArray(A, n, B, m));
}
}
 
// This code is contributed by Prerna Saini


Python3
# Python 3 program to find smallest
# length subarray that contains all
# elements of another array.
 
# function return the minimum length
# of sub_array
def minimumSubArray(A, n, B, m) :
    result = 10000000
  
    # Pick starting point
    for i in range(0, n) :
         
        # Pick ending point
        for j in range(0,n) :
   
            # k is index in first array and
            # 'index' is index in second array.
            index = 0
            for k in range(i, j + 1) :
                if (A[k] == B[index]) :
                    index=index + 1
                if (index == m) :
                    break
                 
            # update minimum length sub_array
            if (index == m and result > k - i + 1) :
                if (k == n) :
                    result =  k - i
                else :
                    result = k - i + 1
         
    # return minimum length subarray
    return result
     
# driver program to test above function
A = [ 5, 6, 5, 2, 7, 5, 6, 7, 5, 5, 7 ]
B = [ 5, 5, 7 ]
n = len(A)
m = len(B)
print(minimumSubArray(A, n, B, m))
 
 
#This code is contributed by Nikita Tiwari


C#
// C# program to find smallest length
// subarray that contains all elements
// of another array.
using System;
 
class GFG {
 
// function return the minimum
// length of sub_array
static int minimumSubArray(int []A, int n,
                           int []B, int m)
{
    int result = int.MaxValue;
 
    // Pick starting point
    for (int i = 0; i < n; i++) {
 
        // Pick ending point
        for (int j = i; j < n; j++) {
 
            // k is index in first array
            // and 'index' is index in
            // second array.
            int k, index = 0;
            for (k = i; k <= j; k++) {
                if (A[k] == B[index])
                    index++;
                if (index == m)
                    break;
            }
 
            // update minimum length sub_array
            if (index == m && result > k - i + 1)
            result = (k == n) ? k - i : k - i + 1;
        }
    }
 
    // return minimum length subarray
    return result;
}
 
  // Driver code
  public static void Main()
  {
    int []A = { 5, 6, 5, 2, 7, 5,
                 6, 7, 5, 5, 7 };
    int []B = { 5, 5, 7 };
    int n = A.Length;
    int m = B.Length;
    Console.Write(minimumSubArray(A, n, B, m));
  }
}
 
// This code is contributed by nitin mittal.


PHP
 $k - $i + 1)
            $result = ($k == $n) ? $k - $i : $k - $i + 1;
        }
    }
 
    // return minimum length subarray
    return $result;
}
 
    // Driver Code
    $A = array(5, 6, 5, 2, 7, 5, 6, 7, 5, 5, 7);
    $B = array(5, 5, 7);
    $n = count($A);
    $m = count($B);
    echo minimumSubArray($A, $n, $B, $m);
     
// This code is contributed by anuj_67
?>


Javascript


C++
// C++ program to find smallest length
// subarray that contains all elements
// of another array.
#include 
using namespace std;
 
// Returns the minimum length of sub_array
int minimumSubArray(int A[], int n, int B[],
                                     int m)
{
    int result = INT_MAX;
 
    // Traverse main_array element
    for (int i = 0; i < n - m + 1; i++)
    {
        // Pick only those subarray of main_array
        // whose first element match with the
        // first element of second_array
        if (A[i] == B[0]) {
         
            // initialize starting of both
            // subarrays
            int j = 0, index = i;
            for (; index < n; index++) {
                if (A[index] == B[j])
                    j++;
 
                // if we found all elements of
                // second array
                if (j == m)
                    break;
            }
 
            // update minimum length sub_array
            if (j == m && result > index - i + 1)
              result = (index == n) ? index - i : index - i + 1;
    }
}
 
    // return minimum length subarray
    return result;
}
 
// driver program to test above function
int main()
{
    int A[] = { 5, 6, 5, 2, 7, 5, 6, 7, 5, 5, 7 };
    int B[] = { 5, 5, 7 };
    int n = sizeof(A)/sizeof(A[0]);
    int m = sizeof(B)/sizeof(B[0]);
    cout << minimumSubArray(A, n, B, m);
    return 0;
}


Java
// Java program to find smallest length
// subarray that contains all elements
// of another array.
import java.io.*;
 
class GFG {
 
// Returns the minimum length of sub_array
static int minimumSubArray(int A[], int n,
                           int B[], int m)
{
    int result = Integer.MAX_VALUE;
 
    // Traverse main_array element
    for (int i = 0; i < n - m + 1; i++)
    {
        // Pick only those subarray of
        // main_array whose first element
        // match with the first element
        // of second_array
        if (A[i] == B[0]) {
         
            // initialize starting of
            // both subarrays
            int j = 0, index = i;
            for (; index < n; index++) {
                if (A[index] == B[j])
                    j++;
 
                // if we found all elements
                // of second array
                if (j == m)
                    break;
            }
 
            // update minimum length sub_array
            if (j == m && result > index - i + 1)
            result = (index == n) ? index - i :
                                 index - i + 1;
    }
 
    }
     
    // return minimum length subarray
    return result;
}
 
// driver program to test above function
public static void main(String[] args)
{
    int A[] = { 5, 6, 5, 2, 7, 5,
                6, 7, 5, 5, 7 };
    int B[] = { 5, 5, 7 };
    int n = A.length;
    int m = B.length;
    System.out.println(minimumSubArray(A, n,
                                     B, m));
}
}
 
// This code is contributed by Prerna Saini


Python3
# Python 3 program to find smallest length
# subarray that contains all elements
# of another array.
 
# Returns the minimum length of sub_array
def minimumSubArray( A,n, B, m) :
    result = 1000000
  
    # Traverse main_array element
    for i in range(0, n - m + 1) :
 
        # Pick only those subarray of main_array
        # whose first element match with the
        # first element of second_array
        if (A[i] == B[0]) :
             
            # initialize starting of both
            # subarrays
            j = 0
            index = i
            for index in range(i, n) :
                if (A[index] == B[j]) :
                    j = j+1
  
                # if we found all elements
                # of second array
                if (j == m) :
                    break
             
            # update minimum length sub_array
            if (j == m and result > index - i + 1) :
                if(index == n) :
                    result = index - i
                else:
                    result = index - i + 1
     
  
    # return minimum length subarray
    return result
     
# driver program to test above function
A = [ 5, 6, 5, 2, 7, 5, 6, 7, 5, 5, 7 ]
B = [ 5, 5, 7 ]
n = len(A)
m = len(B)
print(minimumSubArray(A, n, B, m))
 
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find smallest length
// subarray that contains all elements
// of another array.
using System;
 
class GFG {
 
// Returns the minimum length of sub_array
static int minimumSubArray(int []A, int n,
                           int []B, int m)
{
    int result = int.MaxValue;
 
    // Traverse main_array element
    for (int i = 0; i < n - m + 1; i++)
    {
        // Pick only those subarray of
        // main_array whose first element
        // match with the first element
        // of second_array
        if (A[i] == B[0]) {
         
            // initialize starting of
            // both subarrays
            int j = 0, index = i;
            for (; index < n; index++)
            {
                if (A[index] == B[j])
                    j++;
 
                // if we found all elements
                // of second array
                if (j == m)
                    break;
            }
 
            // update minimum length sub_array
            if (j == m && result > index - i + 1)
            result = (index == n) ? index - i :
                                index - i + 1;
    }
    }
     
    // return minimum length subarray
    return result;
}
 
  // Driver code
  public static void Main()
  {
    int []A = { 5, 6, 5, 2, 7, 5,
                 6, 7, 5, 5, 7 };
    int []B = { 5, 5, 7 };
    int n = A.Length;
    int m = B.Length;
    Console.Write(minimumSubArray(A, n, B, m));
  }
}
 
// This code is contributed by nitin mittal.


PHP
 $index - $i + 1)
            $result = ($index == $n) ?
                         $index - $i :
                         $index - $i + 1;
        }
    }
 
    // return minimum length subarray
    return $result;
}
 
// Driver Code
$A = array(5, 6, 5, 2, 7, 5,
              6, 7, 5, 5, 7 );
$B = array(5, 5, 7 );
$n = sizeof($A);
$m = sizeof($B);
echo(minimumSubArray($A, $n, $B, $m));
 
// This code is contributed
// by Shivi_Aggarwal
?>


Javascript


输出:

3

时间复杂度:O(n 3 )
辅助空间:O(1)

方法二(高效)

方法 2 是方法 1 的优化版本。这里我们只考虑第一个元素与第二个数组的第一个元素匹配的那些子数组。如果第一个元素匹配,那么我们匹配 Main_array 中第二个数组的其余元素,如果所有元素都匹配,那么我们会根据需要更新长度。最后,我们返回 sub_array 的最小长度。
下面是上述想法的实现:

C++

// C++ program to find smallest length
// subarray that contains all elements
// of another array.
#include 
using namespace std;
 
// Returns the minimum length of sub_array
int minimumSubArray(int A[], int n, int B[],
                                     int m)
{
    int result = INT_MAX;
 
    // Traverse main_array element
    for (int i = 0; i < n - m + 1; i++)
    {
        // Pick only those subarray of main_array
        // whose first element match with the
        // first element of second_array
        if (A[i] == B[0]) {
         
            // initialize starting of both
            // subarrays
            int j = 0, index = i;
            for (; index < n; index++) {
                if (A[index] == B[j])
                    j++;
 
                // if we found all elements of
                // second array
                if (j == m)
                    break;
            }
 
            // update minimum length sub_array
            if (j == m && result > index - i + 1)
              result = (index == n) ? index - i : index - i + 1;
    }
}
 
    // return minimum length subarray
    return result;
}
 
// driver program to test above function
int main()
{
    int A[] = { 5, 6, 5, 2, 7, 5, 6, 7, 5, 5, 7 };
    int B[] = { 5, 5, 7 };
    int n = sizeof(A)/sizeof(A[0]);
    int m = sizeof(B)/sizeof(B[0]);
    cout << minimumSubArray(A, n, B, m);
    return 0;
}

Java

// Java program to find smallest length
// subarray that contains all elements
// of another array.
import java.io.*;
 
class GFG {
 
// Returns the minimum length of sub_array
static int minimumSubArray(int A[], int n,
                           int B[], int m)
{
    int result = Integer.MAX_VALUE;
 
    // Traverse main_array element
    for (int i = 0; i < n - m + 1; i++)
    {
        // Pick only those subarray of
        // main_array whose first element
        // match with the first element
        // of second_array
        if (A[i] == B[0]) {
         
            // initialize starting of
            // both subarrays
            int j = 0, index = i;
            for (; index < n; index++) {
                if (A[index] == B[j])
                    j++;
 
                // if we found all elements
                // of second array
                if (j == m)
                    break;
            }
 
            // update minimum length sub_array
            if (j == m && result > index - i + 1)
            result = (index == n) ? index - i :
                                 index - i + 1;
    }
 
    }
     
    // return minimum length subarray
    return result;
}
 
// driver program to test above function
public static void main(String[] args)
{
    int A[] = { 5, 6, 5, 2, 7, 5,
                6, 7, 5, 5, 7 };
    int B[] = { 5, 5, 7 };
    int n = A.length;
    int m = B.length;
    System.out.println(minimumSubArray(A, n,
                                     B, m));
}
}
 
// This code is contributed by Prerna Saini

Python3

# Python 3 program to find smallest length
# subarray that contains all elements
# of another array.
 
# Returns the minimum length of sub_array
def minimumSubArray( A,n, B, m) :
    result = 1000000
  
    # Traverse main_array element
    for i in range(0, n - m + 1) :
 
        # Pick only those subarray of main_array
        # whose first element match with the
        # first element of second_array
        if (A[i] == B[0]) :
             
            # initialize starting of both
            # subarrays
            j = 0
            index = i
            for index in range(i, n) :
                if (A[index] == B[j]) :
                    j = j+1
  
                # if we found all elements
                # of second array
                if (j == m) :
                    break
             
            # update minimum length sub_array
            if (j == m and result > index - i + 1) :
                if(index == n) :
                    result = index - i
                else:
                    result = index - i + 1
     
  
    # return minimum length subarray
    return result
     
# driver program to test above function
A = [ 5, 6, 5, 2, 7, 5, 6, 7, 5, 5, 7 ]
B = [ 5, 5, 7 ]
n = len(A)
m = len(B)
print(minimumSubArray(A, n, B, m))
 
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to find smallest length
// subarray that contains all elements
// of another array.
using System;
 
class GFG {
 
// Returns the minimum length of sub_array
static int minimumSubArray(int []A, int n,
                           int []B, int m)
{
    int result = int.MaxValue;
 
    // Traverse main_array element
    for (int i = 0; i < n - m + 1; i++)
    {
        // Pick only those subarray of
        // main_array whose first element
        // match with the first element
        // of second_array
        if (A[i] == B[0]) {
         
            // initialize starting of
            // both subarrays
            int j = 0, index = i;
            for (; index < n; index++)
            {
                if (A[index] == B[j])
                    j++;
 
                // if we found all elements
                // of second array
                if (j == m)
                    break;
            }
 
            // update minimum length sub_array
            if (j == m && result > index - i + 1)
            result = (index == n) ? index - i :
                                index - i + 1;
    }
    }
     
    // return minimum length subarray
    return result;
}
 
  // Driver code
  public static void Main()
  {
    int []A = { 5, 6, 5, 2, 7, 5,
                 6, 7, 5, 5, 7 };
    int []B = { 5, 5, 7 };
    int n = A.Length;
    int m = B.Length;
    Console.Write(minimumSubArray(A, n, B, m));
  }
}
 
// This code is contributed by nitin mittal.

PHP

 $index - $i + 1)
            $result = ($index == $n) ?
                         $index - $i :
                         $index - $i + 1;
        }
    }
 
    // return minimum length subarray
    return $result;
}
 
// Driver Code
$A = array(5, 6, 5, 2, 7, 5,
              6, 7, 5, 5, 7 );
$B = array(5, 5, 7 );
$n = sizeof($A);
$m = sizeof($B);
echo(minimumSubArray($A, $n, $B, $m));
 
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript


输出:

3

时间复杂度: O(n*n)
辅助空间: O(1)