给定两个数组A[]和B[] ,任务是找到两个给定数组元素之间未交叉线的最大数量。
A straight line can be drawn between two array elements A[i] and B[j] only if:
- A[i] = B[j]
- The line does not intersect any other line.
例子:
Input: A[] = {3, 9, 2}, B[] = {3, 2, 9}
Output: 2
Explanation:
The lines between A[0] to B[0] and A[1] to B[2] does not intersect each other.
Input: A[] = {1, 2, 3, 4, 5}, B[] = {1, 2, 3, 4, 5}
Output: 5
朴素的做法:想法是生成数组A[]的所有子序列,并尝试在数组B[]中找到它们,这样两个子序列可以通过连接直线连接。在 A[] 和 B[] 中发现的最长的此类子序列将具有最大数量的未交叉线。所以打印该子序列的长度。
时间复杂度: O(M * 2 N )
辅助空间: O(1)
高效的方法:从上面的方法可以看出,任务是找到两个数组中共同的最长子序列。因此,可以通过使用动态规划找到两个数组之间的最长公共子序列来优化上述方法。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count maximum number
// of uncrossed lines between the
// two given arrays
int uncrossedLines(int* a, int* b,
int n, int m)
{
// Stores the length of lcs
// obtained upto every index
int dp[n + 1][m + 1];
// Iterate over first array
for (int i = 0; i <= n; i++) {
// Iterate over second array
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0)
// Update value in dp table
dp[i][j] = 0;
// If both characters
// are equal
else if (a[i - 1] == b[j - 1])
// Update the length of lcs
dp[i][j] = 1 + dp[i - 1][j - 1];
// If both characters
// are not equal
else
// Update the table
dp[i][j] = max(dp[i - 1][j],
dp[i][j - 1]);
}
}
// Return the answer
return dp[n][m];
}
// Driver Code
int main()
{
// Given array A[] and B[]
int A[] = { 3, 9, 2 };
int B[] = { 3, 2, 9 };
int N = sizeof(A) / sizeof(A[0]);
int M = sizeof(B) / sizeof(B[0]);
// Function Call
cout << uncrossedLines(A, B, N, M);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to count maximum number
// of uncrossed lines between the
// two given arrays
static int uncrossedLines(int[] a, int[] b,
int n, int m)
{
// Stores the length of lcs
// obtained upto every index
int[][] dp = new int[n + 1][m + 1];
// Iterate over first array
for(int i = 0; i <= n; i++)
{
// Iterate over second array
for(int j = 0; j <= m; j++)
{
if (i == 0 || j == 0)
// Update value in dp table
dp[i][j] = 0;
// If both characters
// are equal
else if (a[i - 1] == b[j - 1])
// Update the length of lcs
dp[i][j] = 1 + dp[i - 1][j - 1];
// If both characters
// are not equal
else
// Update the table
dp[i][j] = Math.max(dp[i - 1][j],
dp[i][j - 1]);
}
}
// Return the answer
return dp[n][m];
}
// Driver Code
public static void main (String[] args)
{
// Given array A[] and B[]
int A[] = { 3, 9, 2 };
int B[] = { 3, 2, 9 };
int N = A.length;
int M = B.length;
// Function call
System.out.print(uncrossedLines(A, B, N, M));
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for
# the above approach
# Function to count maximum number
# of uncrossed lines between the
# two given arrays
def uncrossedLines(a, b,
n, m):
# Stores the length of lcs
# obtained upto every index
dp = [[0 for x in range(m + 1)]
for y in range(n + 1)]
# Iterate over first array
for i in range (n + 1):
# Iterate over second array
for j in range (m + 1):
if (i == 0 or j == 0):
# Update value in dp table
dp[i][j] = 0
# If both characters
# are equal
elif (a[i - 1] == b[j - 1]):
# Update the length of lcs
dp[i][j] = 1 + dp[i - 1][j - 1]
# If both characters
# are not equal
else:
# Update the table
dp[i][j] = max(dp[i - 1][j],
dp[i][j - 1])
# Return the answer
return dp[n][m]
# Driver Code
if __name__ == "__main__":
# Given array A[] and B[]
A = [3, 9, 2]
B = [3, 2, 9]
N = len(A)
M = len(B)
# Function Call
print (uncrossedLines(A, B, N, M))
# This code is contributed by Chitranayal
C#
// C# program for the above approach
using System;
class GFG{
// Function to count maximum number
// of uncrossed lines between the
// two given arrays
static int uncrossedLines(int[] a, int[] b,
int n, int m)
{
// Stores the length of lcs
// obtained upto every index
int[,] dp = new int[n + 1, m + 1];
// Iterate over first array
for(int i = 0; i <= n; i++)
{
// Iterate over second array
for(int j = 0; j <= m; j++)
{
if (i == 0 || j == 0)
// Update value in dp table
dp[i, j] = 0;
// If both characters
// are equal
else if (a[i - 1] == b[j - 1])
// Update the length of lcs
dp[i, j] = 1 + dp[i - 1, j - 1];
// If both characters
// are not equal
else
// Update the table
dp[i, j] = Math.Max(dp[i - 1, j],
dp[i, j - 1]);
}
}
// Return the answer
return dp[n, m];
}
// Driver Code
public static void Main (String[] args)
{
// Given array A[] and B[]
int[] A = { 3, 9, 2 };
int[] B = { 3, 2, 9 };
int N = A.Length;
int M = B.Length;
// Function call
Console.Write(uncrossedLines(A, B, N, M));
}
}
// This code is contributed by code_hunt
}
输出:
2
时间复杂度: O(N*M)
辅助空间: O(N*M)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live