📜  最长递增子序列由来自可被先前选择的索引整除的索引的元素组成

📅  最后修改于: 2021-09-22 10:02:54             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是通过从可被所有先前选择的索引整除的索引中选择元素来找到可能的最长递增子序列的长度。
注意:考虑基于 1 的索引

例子:

朴素方法:最简单的方法是通过从可被序列中所有前面的索引整除的索引序列中进行选择,生成所有可能的数组元素子序列。找出所有这些子序列的长度并打印得到的最大长度。

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

高效的方法:为了优化上述方法,思想是使用动态规划。请按照以下步骤解决问题:

  • 初始化大小为N的数组dp[]dp[]表中的第i索引,即dp[i] ,表示直到i索引为止获得的所需类型的最长可能子序列的长度。
  • 遍历数组DP []使用变量i,和横动至i的与变量j所有的倍数,使得2 * I≤Ĵ≤ñ。
    • 对于每个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


Javascript


输出:
4

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

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