通过删除顺序为数组 B 的子序列,最小化删除排列 A 的所有元素的操作
给定前N个自然数的两个排列数组A[]和B[] ,任务是找到删除所有数组元素A[]所需的最小操作数,以便在每个操作中删除数组元素A[ ]的顺序与数组B[]中的顺序相同。
例子:
Input: A[] = { 4, 2, 1, 3 }, B[] = { 1, 3, 2, 4 }
Output: 3
Explanation:
The given example can be solved by following the given steps:
- During the 1st operation, integers at index 2 and 3 in the array A[] can be deleted. Hence, the array A[] = {4, 2}.
- During the 2st operation, integer at index 1 in the array A[] can be deleted. Hence, the array A[] = {4}.
- During the 3rd operation, integer at index 0 in the array A[] can be deleted. Hence, the array A[] = {}.
The order in which the elements are deleted is {1, 3, 2, 4} which is equal to B. Hence a minimum of 3 operations is required.
Input: A[] = {2, 4, 6, 1, 5, 3}, B[] = {6, 5, 4, 2, 3, 1}
Output: 4
方法:给定的问题可以使用以下讨论的步骤来解决:
- 创建两个变量i和j ,其中i跟踪B的当前要删除的元素的索引, j跟踪A的当前元素。最初, i=0和j=0 。
- 使用j遍历排列数组A[]以获取范围[0, N-1]中的所有j值。如果A[j] = B[i] ,将i的值增加 1 并继续遍历数组A[] 。
- 在数组A[]被完全遍历后,递增cnt变量的值,该变量维护所需操作的计数。
- 重复步骤2和3直到i
。 - 完成以上步骤后, 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)