📌  相关文章
📜  最小化要添加到给定数组的元素,使其包含另一个给定数组作为其子序列

📅  最后修改于: 2021-09-17 16:03:18             🧑  作者: Mango

给定一个由N 个不同整数组成的数组A[]和另一个由M 个整数组成的数组B[] ,任务是找到要添加到数组B[] 中的最小元素数,使得数组A[]成为数组B[] 的子序列。

例子:

朴素的方法:朴素的方法是生成数组B 的所有子序列,然后找到该子序列,以便从数组A 中添加最少数量的元素使其等于数组A 。打印添加的元素的最小计数。
时间复杂度: O(N*2 M )
辅助空间: O(M+N)

高效方法:上述方法可以使用动态规划进行优化。这个想法是在给定的两个数组AB之间找到最长公共子序列。主要观察结果是,可以通过从数组A[]的长度中减去最长公共子序列的长度来找到要在B[] 中添加以使A[]成为其子序列的最小元素数。

因此,数组A[]的长度与最长公共子序列的长度之差就是需要的结果。

下面是上述方法的实现:

C++14
// C++14 program for the above approach
#include 
using namespace std;
 
// Function that finds the minimum number
// of the element must be added to make A
// as a subsequence in B
int transformSubsequence(int n, int m,
                         vector A,
                         vector B)
{
     
    // Base Case
    if (B.size() == 0)
        return n;
 
    // dp[i][j] indicates the length of
    // LCS of A of length i & B of length j
    vector> dp(n + 1,
           vector(m + 1, 0));
 
    for(int i = 0; i < n + 1; i++)
    {
        for(int j = 0; j < m + 1; j++)
        {
             
            // If there are no elements
            // either in A or B then the
            // length of lcs is 0
            if (i == 0 or j == 0)
                dp[i][j] = 0;
 
            // If the element present at
            // ith and jth index of A and B
            // are equal then include in LCS
            else if (A[i - 1] == B[j - 1])
                dp[i][j] = 1 + dp[i - 1][j - 1];
 
            // If they are not equal then
            // take the max
            else
                dp[i][j] = max(dp[i - 1][j],
                               dp[i][j - 1]);
        }
    }
 
    // Return difference of length
    // of A and lcs of A and B
    return n - dp[n][m];
}
 
// Driver Code
int main()
{
    int N = 5;
    int M = 6;
 
    // Given sequence A and B
    vector A = { 1, 2, 3, 4, 5 };
    vector B = { 2, 5, 6, 4, 9, 12 };
 
    // Function call
    cout << transformSubsequence(N, M, A, B);
 
    return 0;
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function that finds the minimum number
// of the element must be added to make A
// as a subsequence in B
static int transformSubsequence(int n, int m,
                                int []A, int []B)
{
  // Base Case
  if (B.length == 0)
    return n;
 
  // dp[i][j] indicates the length of
  // LCS of A of length i & B of length j
  int [][]dp = new int[n + 1][m + 1];
 
  for(int i = 0; i < n + 1; i++)
  {
    for(int j = 0; j < m + 1; j++)
    {
      // If there are no elements
      // either in A or B then the
      // length of lcs is 0
      if (i == 0 || j == 0)
        dp[i][j] = 0;
 
      // If the element present at
      // ith and jth index of A and B
      // are equal then include in LCS
      else if (A[i - 1] == B[j - 1])
        dp[i][j] = 1 + dp[i - 1][j - 1];
 
      // If they are not equal then
      // take the max
      else
        dp[i][j] = Math.max(dp[i - 1][j],
                            dp[i][j - 1]);
    }
  }
 
  // Return difference of length
  // of A and lcs of A and B
  return n - dp[n][m];
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 5;
  int M = 6;
 
  // Given sequence A and B
  int []A = {1, 2, 3, 4, 5};
  int []B = {2, 5, 6, 4, 9, 12};
 
  // Function call
  System.out.print(transformSubsequence(N, M, A, B));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function that finds the minimum number
# of the element must be added to make A
# as a subsequence in B
def transformSubsequence(n, m, A, B):
 
    # Base Case
    if B is None or len(B) == 0:
        return n
 
    # dp[i][j] indicates the length of
    # LCS of A of length i & B of length j
    dp = [[0 for col in range(m + 1)]
        for row in range(n + 1)]
 
    for i in range(n + 1):
 
        for j in range(m + 1):
 
            # If there are no elements
            # either in A or B then the
            # length of lcs is 0
            if i == 0 or j == 0:
                dp[i][j] = 0
 
            # If the element present at
            # ith and jth index of A and B
            # are equal then include in LCS
            elif A[i-1] == B[j-1]:
                dp[i][j] = 1 + dp[i-1][j-1]
 
            # If they are not equal then
            # take the max
            else:
                dp[i][j] = max(dp[i-1][j], dp[i][j-1])
 
    # Return difference of length
    # of A and lcs of A and B
    return n - dp[n][m]
 
 
# Driver Code
if __name__ == "__main__":
 
    N = 5
    M = 6
     
    # Given Sequence A and B
    A = [1, 2, 3, 4, 5]
    B = [2, 5, 6, 4, 9, 12]
 
    # Function Call
    print(transformSubsequence(N, M, A, B))


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function that finds the minimum number
// of the element must be added to make A
// as a subsequence in B
static int transformSubsequence(int n, int m,
                                int []A, int []B)
{
  // Base Case
  if (B.Length == 0)
    return n;
 
  // dp[i,j] indicates the length of
  // LCS of A of length i & B of length j
  int [,]dp = new int[n + 1, m + 1];
 
  for(int i = 0; i < n + 1; i++)
  {
    for(int j = 0; j < m + 1; j++)
    {
      // If there are no elements
      // either in A or B then the
      // length of lcs is 0
      if (i == 0 || j == 0)
        dp[i, j] = 0;
 
      // If the element present at
      // ith and jth index of A and B
      // are equal then include in LCS
      else if (A[i - 1] == B[j - 1])
        dp[i, j] = 1 + dp[i - 1, j - 1];
 
      // If they are not equal then
      // take the max
      else
        dp[i, j] = Math.Max(dp[i - 1, j],
                            dp[i, j - 1]);
    }
  }
 
  // Return difference of length
  // of A and lcs of A and B
  return n - dp[n, m];
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 5;
  int M = 6;
 
  // Given sequence A and B
  int []A = {1, 2, 3, 4, 5};
  int []B = {2, 5, 6, 4, 9, 12};
 
  // Function call
  Console.Write(transformSubsequence(N, M,
                                     A, B));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
3

时间复杂度: O(M*M),其中 N 和 M 分别是数组 A[] 和 B[] 的长度。
辅助空间: O(M*N)

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