给定一个大小为 N 的数组 arr[]。任务是从给定数组中找到最长子序列的长度,使得该序列严格递增并且没有两个相邻元素互质。
注意:给定数组中的元素严格按顺序递增 (1 <= a[i] <= 10 5 )
例子:
Input : a[] = { 1, 2, 3, 4, 5, 6}
Output : 3
Explanation : Possible sub sequences are {1}, {2}, {3}, {4}, {5}, {6}, {2, 4}, {2, 4, 6}, {2, 6}, {4, 6}, {3, 6}.
The subsequence {2, 4, 6} has the longest length.
Input : a[] = { 1, 1, 1, 1}
Output : 1
方法:主要思想是使用动态规划的概念。让我们将 dp[x] 定义为最后一个元素为 x 的子序列长度的最大值,并将 d[i] 定义为(dp[x] 的最大值,其中 x 可被 i 整除)。
我们应该按照 x 的递增顺序计算 dp[x]。 dp[x] 的值是(d[i] 的最大值,其中 i 是 x 的因数)+1。在我们计算 dp[x] 之后,对于 x 的每个因数 i,我们也应该更新 d[i] .该算法在 O(N*logN) 中有效,因为从 1 到 N 的除数总数为 O(N*logN)。
注意:有一个角落案例。当集合为 {1} 时,您应该输出 1。
下面是上述方法的实现:
C++
// CPP program to find the length of the
// longest increasing sub sequence from the given array
// such that no two adjacent elements are co prime
#include
using namespace std;
#define N 100005
// Function to find the length of the
// longest increasing sub sequence from the given array
// such that no two adjacent elements are co prime
int LIS(int a[], int n)
{
// To store dp and d value
int dp[N], d[N];
// To store required answer
int ans = 0;
// For all elements in the array
for (int i = 0; i < n; i++) {
// Initially answer is one
dp[a[i]] = 1;
// For all it's divisors
for (int j = 2; j * j <= a[i]; j++) {
if (a[i] % j == 0) {
// Update the dp value
dp[a[i]] = max(dp[a[i]], dp[d[j]] + 1);
dp[a[i]] = max(dp[a[i]], dp[d[a[i] / j]] + 1);
// Update the divisor value
d[j] = a[i];
d[a[i] / j] = a[i];
}
}
// Check for required answer
ans = max(ans, dp[a[i]]);
// Update divisor of a[i]
d[a[i]] = a[i];
}
// Return required answer
return ans;
}
// Driver code
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(a) / sizeof(a[0]);
cout << LIS(a, n);
return 0;
}
Java
// Java program to find the length
// of the longest increasing sub
// sequence from the given array
// such that no two adjacent
// elements are co prime
class GFG
{
static int N=100005;
// Function to find the length of the
// longest increasing sub sequence
// from the given array such that
// no two adjacent elements are co prime
static int LIS(int a[], int n)
{
// To store dp and d value
int dp[]=new int[N], d[]=new int[N];
// To store required answer
int ans = 0;
// For all elements in the array
for (int i = 0; i < n; i++)
{
// Initially answer is one
dp[a[i]] = 1;
// For all it's divisors
for (int j = 2; j * j <= a[i]; j++)
{
if (a[i] % j == 0)
{
// Update the dp value
dp[a[i]] = Math.max(dp[a[i]], dp[d[j]] + 1);
dp[a[i]] = Math.max(dp[a[i]], dp[d[a[i] / j]] + 1);
// Update the divisor value
d[j] = a[i];
d[a[i] / j] = a[i];
}
}
// Check for required answer
ans = Math.max(ans, dp[a[i]]);
// Update divisor of a[i]
d[a[i]] = a[i];
}
// Return required answer
return ans;
}
// Driver code
public static void main(String args[])
{
int a[] = { 1, 2, 3, 4, 5, 6 };
int n = a.length;
System.out.print( LIS(a, n));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to find the length of the
# longest increasing sub sequence from the
# given array such that no two adjacent
# elements are co prime
N = 100005
# Function to find the length of the
# longest increasing sub sequence from
# the given array such that no two
# adjacent elements are co prime
def LIS(a, n):
# To store dp and d value
dp = [0 for i in range(N)]
d = [0 for i in range(N)]
# To store required answer
ans = 0
# For all elements in the array
for i in range(n):
# Initially answer is one
dp[a[i]] = 1
# For all it's divisors
for j in range(2, a[i]):
if j * j > a[i]:
break
if (a[i] % j == 0):
# Update the dp value
dp[a[i]] = max(dp[a[i]],
dp[d[j]] + 1)
dp[a[i]] = max(dp[a[i]],
dp[d[a[i] // j]] + 1)
# Update the divisor value
d[j] = a[i]
d[a[i] // j] = a[i]
# Check for required answer
ans = max(ans, dp[a[i]])
# Update divisor of a[i]
d[a[i]] = a[i]
# Return required answer
return ans
# Driver code
a = [1, 2, 3, 4, 5, 6]
n = len(a)
print(LIS(a, n))
# This code is contributed by mohit kumar
C#
// C# program to find the length
// of the longest increasing sub
// sequence from the given array
// such that no two adjacent
// elements are co prime
using System;
class GFG
{
static int N = 100005;
// Function to find the length of the
// longest increasing sub sequence
// from the given array such that
// no two adjacent elements are co prime
static int LIS(int []a, int n)
{
// To store dp and d value
int []dp = new int[N];
int []d = new int[N];
// To store required answer
int ans = 0;
// For all elements in the array
for (int i = 0; i < n; i++)
{
// Initially answer is one
dp[a[i]] = 1;
// For all it's divisors
for (int j = 2; j * j <= a[i]; j++)
{
if (a[i] % j == 0)
{
// Update the dp value
dp[a[i]] = Math.Max(dp[a[i]], dp[d[j]] + 1);
dp[a[i]] = Math.Max(dp[a[i]], dp[d[a[i] / j]] + 1);
// Update the divisor value
d[j] = a[i];
d[a[i] / j] = a[i];
}
}
// Check for required answer
ans = Math.Max(ans, dp[a[i]]);
// Update divisor of a[i]
d[a[i]] = a[i];
}
// Return required answer
return ans;
}
// Driver code
public static void Main()
{
int []a = { 1, 2, 3, 4, 5, 6 };
int n = a.Length;
Console.WriteLine(LIS(a, n));
}
}
// This code is contributed by Ryuga
PHP
Javascript
3
时间复杂度: O(N* log(N))
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。