📌  相关文章
📜  通过重复删除最后一个元素并将其放置在任意索引处,将数组转换为另一个数组

📅  最后修改于: 2021-10-25 06:49:25             🧑  作者: Mango

给定两个数组A[]B[] ,它们都由前N 个自然数的排列组成,任务是计算需要将最后一个数组元素移动到数组A[ 中任意位置的最小次数]使数组A[]B[]相等。

例子:

方法:给定的问题可以通过找到第一个排列的前i个连续元素来解决,该元素与第二个排列的子序列相同,那么操作次数必须至少少(N – I ) ,因为最后一个(N – i) 个元素可以被最佳选择并插入到所需的索引处。因此, (N – i)是将数组A[]转换为B[]所需的最小步骤数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the minimum number
// of operations required to convert
// the array A[] into array B[]
int minCount(int A[], int B[], int N)
{
    // Stores the index in the first
    // permutation A[] which is same
    // as the subsequence in B[]
    int i = 0;
 
    // Find the first i elements in A[]
    // which is a subsequence in B[]
    for (int j = 0; j < N; j++) {
 
        // If element A[i]
        // is same as B[j]
        if (A[i] == B[j]) {
            i++;
        }
    }
 
    // Return the count of
    // operations required
    return N - i;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 2, 3, 4, 5 };
    int B[] = { 1, 5, 2, 3, 4 };
 
    int N = sizeof(A) / sizeof(A[0]);
 
    cout << minCount(A, B, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to count the minimum number
// of operations required to convert
// the array A[] into array B[]
static int minCount(int A[], int B[], int N)
{
     
    // Stores the index in the first
    // permutation A[] which is same
    // as the subsequence in B[]
    int i = 0;
 
    // Find the first i elements in A[]
    // which is a subsequence in B[]
    for(int j = 0; j < N; j++)
    {
         
        // If element A[i]
        // is same as B[j]
        if (A[i] == B[j])
        {
            i++;
        }
    }
 
    // Return the count of
    // operations required
    return N - i;
}
 
// Driver Code
public static void main (String[] args)
{
    int A[] = { 1, 2, 3, 4, 5 };
    int B[] = { 1, 5, 2, 3, 4 };
    int N = A.length;
 
    System.out.println(minCount(A, B, N));
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to count the minimum number
# of operations required to convert
# the array A[] into array B[]
def minCount(A, B, N):
     
    # Stores the index in the first
    # permutation A[] which is same
    # as the subsequence in B[]
    i = 0
 
    # Find the first i elements in A[]
    # which is a subsequence in B[]
    for j in range(N):
         
        # If element A[i]
        # is same as B[j]
        if (A[i] == B[j]):
            i += 1
 
    # Return the count of
    # operations required
    return N - i
 
# Driver Code
if __name__ == '__main__':
     
    A = [ 1, 2, 3, 4, 5 ]
    B = [ 1, 5, 2, 3, 4 ]
 
    N = len(A)
 
    print(minCount(A, B, N))
     
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the minimum number
// of operations required to convert
// the array A[] into array B[]
static int minCount(int[] A, int[] B, int N)
{
     
    // Stores the index in the first
    // permutation A[] which is same
    // as the subsequence in B[]
    int i = 0;
 
    // Find the first i elements in A[]
    // which is a subsequence in B[]
    for(int j = 0; j < N; j++)
    {
         
        // If element A[i]
        // is same as B[j]
        if (A[i] == B[j])
        {
            i++;
        }
    }
 
    // Return the count of
    // operations required
    return N - i;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] A = { 1, 2, 3, 4, 5 };
    int[] B = { 1, 5, 2, 3, 4 };
    int N = A.Length;
 
    Console.WriteLine(minCount(A, B, N));
}
}
 
// This code is contributed by ukasp


Javascript


输出:
1

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

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