📜  最长分割子序列

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

给定一个大小为 N 的数组 A。你的任务是找到最大分割子序列的长度。分割序列是一个序列 a1, a2, …, aN 其中每当 i < j 时,ai 将 j 整除。例如,3、15、60、720 是一个除法序列。
输入-
每个测试用例的第一行是 N,其中 N 是数组的大小。
每个测试用例的第二行包含 N 个空格分隔的整数,这是数组的输入。
输出-
最大划分子序列的长度
例子:

这个问题只是最长递增子序列的一种变体。我们可以使用动态规划来解决这个问题。这个想法是找到以每个元素结尾的最长分割子序列,最后返回所有元素中的最大值。

C++
/* Dynamic Programming C++ implementation of lds problem */
#include
using namespace std;
   
/* lds() returns the length of the longest dividing
  subsequence in arr[] of size n */
int lds( int arr[], int n )
{
    int lds[n];
  
    lds[0] = 1;  
 
    /* Compute optimized lds values in bottom up manner */
    for (int i = 1; i < n; i++ )
    {
        lds[i] = 1;
        for (int j = 0; j < i; j++ ) 
            if (lds[j] != 0 && arr[i] % arr[j] == 0)
                lds[i] = max(lds[i], lds[j] + 1);
    }
 
    // Return maximum value in lds[]
    return *max_element(lds, lds+n);
}
   
/* Driver program to test above function */
int main()
{
    int arr[] = { 2, 11, 16, 12, 36, 60, 71, 17,
                     29, 144, 288, 129, 432, 993};
    int n = sizeof(arr)/sizeof(arr[0]);
    printf("Length of lds is %d\n", lds( arr, n ) );
    return 0;
}


Java
/* Dynamic Programming Java implementation of lds problem */
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
/* lds() returns the length of the longest dividing
  subsequence in arr[] of size n */
static int lds( Integer arr[], int n )
{
    Integer lds[]=new Integer[n];
   
    lds[0] = 1;  
  
    /* Compute optimized lds values in bottom up manner */
    for (int i = 1; i < n; i++ )
    {
        lds[i] = 1;
        for (int j = 0; j < i; j++ ) 
            if (lds[j] != 0 && arr[i] % arr[j] == 0)
                lds[i] = Math.max(lds[i], lds[j] + 1);
    }
  
    // Return maximum value in lds[]
    int max=(int)Collections.max(Arrays.asList(lds));
    return max;
}
    
/* Driver program to test above function */
public static void main(String args[])
{
    Integer arr[] = { 2, 11, 16, 12, 36, 60, 71, 17,
                     29, 144, 288, 129, 432, 993};
    int n =arr.length ;
    System.out.println("Length of lds is "+lds( arr, n ) );
}
}


Python3
# Dynamic Programming Python3
# implementation of lds problem
 
# lds() returns the length of the longest
# dividing subsequence in arr[] of size n
def lds(arr, n):
     
    lds = [0 for i in range(n)]
     
    lds[0] = 1
     
    # Compute optimized lds values
    # in bottom up manner
    for i in range(n):
        lds[i] = 1
        for j in range(i):
            if (lds[j] != 0 and
                arr[i] % arr[j] == 0):
                lds[i] = max(lds[i], lds[j] + 1)
 
    return max(lds)
 
# Driver Code
arr = [2, 11, 16, 12, 36, 60, 71, 17,
         29, 144, 288, 129, 432, 993]
 
print("Length of lds is",
       lds(arr, len(arr)))
 
# This code is contributed
# by Mohit Kumar


C#
/* Dynamic Programming C# implementation of lds problem */
using System;
using System.Linq;
public class GFG{
    /* lds() returns the length of the longest dividing
      subsequence in arr[] of size n */
    static int lds( int []arr, int n )
    {
        int []lds=new int[n];
 
        lds[0] = 1;  
 
        /* Compute optimized lds values in bottom up manner */
        for (int i = 1; i < n; i++ )
        {
            lds[i] = 1;
            for (int j = 0; j < i; j++ ) 
                if (lds[j] != 0 && arr[i] % arr[j] == 0)
                    lds[i] = Math.Max(lds[i], lds[j] + 1);
        }
 
        // Return maximum value in lds[]
        int max=lds.Max();
        return max;
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        int []arr = { 2, 11, 16, 12, 36, 60, 71, 17,
                         29, 144, 288, 129, 432, 993};
        int n =arr.Length ;
        Console.Write("Length of lds is "+lds( arr, n ) );
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Length of lds is 5

时间复杂度: O(N*N)

辅助空间: O(N)

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