📌  相关文章
📜  通过删除顺序为数组 B 的子序列,最小化删除排列 A 的所有元素的操作

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

通过删除顺序为数组 B 的子序列,最小化删除排列 A 的所有元素的操作

给定前N自然数的两个排列数组A[]B[] ,任务是找到删除所有数组元素A[]所需的最小操作数,以便在每个操作中删除数组元素A[ ]的顺序与数组B[]中的顺序相同。

例子:

方法:给定的问题可以使用以下讨论的步骤来解决:

  1. 创建两个变量ij ,其中i跟踪B的当前要删除的元素的索引, j跟踪A的当前元素。最初, i=0j=0
  2. 使用j遍历排列数组A[]以获取范围[0, N-1]中的所有j值。如果A[j] = B[i] ,将i的值增加 1 并继续遍历数组A[]
  3. 在数组A[]被完全遍历后,递增cnt变量的值,该变量维护所需操作的计数。
  4. 重复步骤23直到i
  5. 完成以上步骤后, cnt中存储的值就是需要的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number of
// operations to delete all elements of
// permutation A in order described by B
int minOperations(int A[], int B[], int N)
{
    // Stores the count of operations
    int cnt = 0;
 
    // Stores the index of current integer
    // in B to be deleted
    int i = 0;
 
    // Loop to iterate over all values of B
    while (i < N) {
 
        // Stores the current index in A
        int j = 0;
 
        // Iterate over all values A
        while (j < N && i < N) {
 
            // If current integer of B and A
            // equal, increment the index of
            // the current integer of B
            if (B[i] == A[j]) {
                i++;
            }
            j++;
        }
 
        // As the permutation A has been
        // traversed completelly, increment
        // the count of operations by 1
        cnt++;
    }
 
    // Return Answer
    return cnt;
}
 
// Driver Code
int main()
{
    int A[] = { 2, 4, 6, 1, 5, 3 };
    int B[] = { 6, 5, 4, 2, 3, 1 };
    int N = sizeof(A) / sizeof(A[0]);
 
    cout << minOperations(A, B, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the minimum number of
// operations to delete all elements of
// permutation A in order described by B
static int minOperations(int A[], int B[], int N)
{
   
    // Stores the count of operations
    int cnt = 0;
 
    // Stores the index of current integer
    // in B to be deleted
    int i = 0;
 
    // Loop to iterate over all values of B
    while (i < N) {
 
        // Stores the current index in A
        int j = 0;
 
        // Iterate over all values A
        while (j < N && i < N) {
 
            // If current integer of B and A
            // equal, increment the index of
            // the current integer of B
            if (B[i] == A[j]) {
                i++;
            }
            j++;
        }
 
        // As the permutation A has been
        // traversed completely, increment
        // the count of operations by 1
        cnt++;
    }
 
    // Return Answer
    return cnt;
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 2, 4, 6, 1, 5, 3 };
    int B[] = { 6, 5, 4, 2, 3, 1 };
    int N = A.length;
 
    System.out.print(minOperations(A, B, N));
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program for the above approach
 
# Function to find the minimum number of
# operations to delete all elements of
# permutation A in order described by B
def minOperations(A, B, N):
   
    # Stores the count of operations
    cnt = 0
 
    # Stores the index of current integer
    # in B to be deleted
    i = 0
 
    # Loop to iterate over all values of B
    while(i < N):
       
        # Stores the current index in A
        j = 0
 
        # Iterate over all values A
        while (j < N and i < N):
 
            # If current integer of B and A
            # equal, increment the index of
            # the current integer of B
            if (B[i] == A[j]):
                i += 1
            j += 1
 
        # As the permutation A has been
        # traversed completely, increment
        # the count of operations by 1
        cnt += 1
 
    # Return Answer
    return cnt
 
# Driver Code
if __name__ == '__main__':
    A = [2, 4, 6, 1, 5, 3]
    B = [6, 5, 4, 2, 3, 1]
    N = len(A)
 
    print(minOperations(A, B, N))
 
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
class GFG {
 
    // Function to find the minimum number of
    // operations to delete all elements of
    // permutation A in order described by B
    static int minOperations(int[] A, int[] B, int N)
    {
 
        // Stores the count of operations
        int cnt = 0;
 
        // Stores the index of current integer
        // in B to be deleted
        int i = 0;
 
        // Loop to iterate over all values of B
        while (i < N) {
 
            // Stores the current index in A
            int j = 0;
 
            // Iterate over all values A
            while (j < N && i < N) {
 
                // If current integer of B and A
                // equal, increment the index of
                // the current integer of B
                if (B[i] == A[j]) {
                    i++;
                }
                j++;
            }
 
            // As the permutation A has been
            // traversed completely, increment
            // the count of operations by 1
            cnt++;
        }
 
        // Return Answer
        return cnt;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] A = { 2, 4, 6, 1, 5, 3 };
        int[] B = { 6, 5, 4, 2, 3, 1 };
        int N = A.Length;
 
        Console.WriteLine(minOperations(A, B, N));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
4

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