给定一个由N个正整数组成的数组arr [] ,任务是通过从所有先前选择的索引都可整除的索引中选择元素来找到最长的递增子序列的长度。
注意:考虑基于1的索引
例子:
Input: arr[] = {1, 4, 2, 3, 6, 4, 9}
Output: 3
Explanation: The optimal way is to select elements present at indices 1, 3 & 6. The sequence {1, 2, 4} generated by selecting elements from these indices is increasing and every index is divisible by the previously selected indices.
Therefore, the length is 3.
Input: arr[] = {2, 3, 4, 5, 6, 7, 8, 9}
Output: 4
Explanation: The optimal way is to select elements present at indices 1, 2, 4 & 8. The sequence {2, 3, 5, 9} generated by selecting elements from these indices is increasing and every index is divisible by the previously selected indices.
Therefore, the length is 4.
天真的方法:最简单的方法是通过从索引序列中进行选择,以生成数组元素的所有子序列,这些索引序列可以被序列中的所有先前索引整除。找到所有这些子序列的长度,并打印获得的最大长度。
时间复杂度: O(2 N )
辅助空间: O(1)
高效方法:为了优化上述方法,其思想是使用动态编程。请按照以下步骤解决问题:
- 初始化大小为N的数组dp [] 。 dp []表中的第i个索引(即dp [i] )表示直到第i个索引所获得的所需类型的最长可能子序列的长度。
- 使用变量i遍历数组dp []并遍历i与变量j的所有倍数,以使2 * i≤j≤N 。
- 对于每个j ,如果arr [j]> arr [i] ,则更新值dp [j] = max(dp [j],dp [i] +1)以包括最长子序列的长度。
- 否则,请检查下一个索引。
- 完成上述步骤后,将数组dp []中的最大元素打印为最长子序列的长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find length of longest
// subsequence generated that
// satisfies the specified conditions
int findMaxLength(int N, vector arr)
{
// Stores the length of longest
// subsequences of all lengths
vector dp(N + 1, 1);
// Iterate through the given array
for (int i = 1; i <= N; i++) {
// Iterate through the multiples i
for (int j = 2 * i; j <= N; j += i) {
if (arr[i - 1] < arr[j - 1]) {
// Update dp[j] as maximum
// of dp[j] and dp[i] + 1
dp[j] = max(dp[j], dp[i] + 1);
}
}
}
// Return the maximum element in dp[]
// as the length of longest subsequence
return *max_element(dp.begin(), dp.end());
}
// Driver Code
int main()
{
vector arr{ 2, 3, 4, 5, 6, 7, 8, 9 };
int N = arr.size();
// Function Call
cout << findMaxLength(N, arr);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find length of longest
// subsequence generated that
// satisfies the specified conditions
static int findMaxLength(int N, int[] arr)
{
// Stores the length of longest
// subsequences of all lengths
int[] dp = new int[N + 1];
Arrays.fill(dp, 1);
// Iterate through the given array
for(int i = 1; i <= N; i++)
{
// Iterate through the multiples i
for(int j = 2 * i; j <= N; j += i)
{
if (arr[i - 1] < arr[j - 1])
{
// Update dp[j] as maximum
// of dp[j] and dp[i] + 1
dp[j] = Math.max(dp[j], dp[i] + 1);
}
}
}
// Return the maximum element in dp[]
// as the length of longest subsequence
return Arrays.stream(dp).max().getAsInt();
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 3, 4, 5, 6, 7, 8, 9 };
int N = arr.length;
// Function Call
System.out.print(findMaxLength(N, arr));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to find length of longest
# subsequence generated that
# satisfies the specified conditions
def findMaxLength(N, arr):
# Stores the length of longest
# subsequences of all lengths
dp = [1] * (N + 1)
# Iterate through the given array
for i in range(1, N + 1):
# Iterate through the multiples i
for j in range(2 * i, N + 1, i):
if (arr[i - 1] < arr[j - 1]):
# Update dp[j] as maximum
# of dp[j] and dp[i] + 1
dp[j] = max(dp[j], dp[i] + 1)
# Return the maximum element in dp[]
# as the length of longest subsequence
return max(dp)
# Driver Code
if __name__ == '__main__':
arr=[2, 3, 4, 5, 6, 7, 8, 9]
N = len(arr)
# Function Call
print(findMaxLength(N, arr))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG{
// Function to find length of longest
// subsequence generated that
// satisfies the specified conditions
static int findMaxLength(int N, int[] arr)
{
// Stores the length of longest
// subsequences of all lengths
int[] dp = new int[N + 1];
for(int i = 1; i <= N; i++)
{
dp[i] = 1;
}
// Iterate through the given array
for(int i = 1; i <= N; i++)
{
// Iterate through the multiples i
for(int j = 2 * i; j <= N; j += i)
{
if (arr[i - 1] < arr[j - 1])
{
// Update dp[j] as maximum
// of dp[j] and dp[i] + 1
dp[j] = Math.Max(dp[j], dp[i] + 1);
}
}
}
// Return the maximum element in dp[]
// as the length of longest subsequence
return dp.Max();;
}
// Driver Code
public static void Main()
{
int[] arr = { 2, 3, 4, 5, 6, 7, 8, 9 };
int N = arr.Length;
// Function Call
Console.WriteLine(findMaxLength(N, arr));
}
}
// This code is contributed by susmitakundugoaldanga
4
时间复杂度: O(N * log N)
辅助空间: O(N)