给定N个正整数的数组arr [] ,任务是找到arr []的最大严格增加的子序列,以使arr []中所选元素的索引以及所选元素分别是彼此的倍数。
注意:考虑对数组arr []基于1的索引。
例子:
Input: arr[] = {1, 4, 2, 3, 6, 4, 9}
Output: 3
Explanation:
We can choose index 1, 3, 6 and values are 1, 2, 4:
Here every greater index is divisible by smaller index and every greater index value is greater than the smaller index value.
Input: arr[] = {5, 3, 4, 6}
Output: 2
Explanation:
We can choose index 1 and 3 and values are 3 and 6:
Here, every greater index is divisible by smaller index and every greater index value is greater than the smaller index value.
天真的方法:
天真的方法是简单地生成所有可能的子序列,并对每个子序列检查两个条件:
- 首先检查元素是否按照严格的顺序排列,以及
- 其次,检查arr []中所选元素的索引是否彼此倍数。
在满足给定两个条件的所有可能子序列中,选择最大的子序列。
时间复杂度: O(N * 2 N )
辅助空间: O(N)
高效方法:
我们可以通过使用动态编程来优化代码,方法是通过缓存其结果来避免重复子问题的冗余计算。
- 创建一个大小等于arr []大小的数组dp [] ,其中dp [i]表示满足给定条件的第i个索引之前最大子序列的大小。
- 用0初始化数组dp [] 。
- 现在,从末尾迭代数组arr [] 。
- 对于每个索引,找到将当前索引除以索引j ,然后检查当前索引处的值是否大于索引j处的元素。
- 如果是,则将dp [j]更新为:
dp[j] = max(dp[current] + 1, dp[j])
- 最后,遍历数组dp []并打印最大值。
下面是有效方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that print maximum length
// of array
void maxLength(int arr[], int n)
{
// dp[] array to store the
// maximum length
vector dp(n, 1);
for (int i = n - 1; i > 1; i--) {
// Find all divisors of i
for (int j = 1;
j <= sqrt(i); j++) {
if (i % j == 0) {
int s = i / j;
if (s == j) {
// If the current value
// is greater than the
// divisor's value
if (arr[i] > arr[s]) {
dp[s] = max(dp[i] + 1,
dp[s]);
}
}
else {
// If current value
// is greater
// than the divisor's value
// and s is not equal
// to current index
if (s != i
&& arr[i] > arr[s])
dp[s] = max(dp[i] + 1,
dp[s]);
// Condition if current
// value is greater
// than the divisor's value
if (arr[i] > arr[j]) {
dp[j] = max(dp[i] + 1,
dp[j]);
}
}
}
}
}
int max = 0;
// Computing the greatest value
for (int i = 1; i < n; i++) {
if (dp[i] > max)
max = dp[i];
}
// Printing maximum length of array
cout << max << "\n";
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 0, 1, 4, 2, 3, 6, 4, 9 };
int size = sizeof(arr) / sizeof(arr[0]);
// Function Call
maxLength(arr, size);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.io.*;
class GFG{
// Function that print maximum length
// of array
static void maxLength(int arr[], int n)
{
// dp[] array to store the
// maximum length
int dp[] = new int[n];
for(int i = 1; i < n; i++)
{
dp[i] = 1;
}
for(int i = n - 1; i > 1; i--)
{
// Find all divisors of i
for(int j = 1;
j <= Math.sqrt(i); j++)
{
if (i % j == 0)
{
int s = i / j;
if (s == j)
{
// If the current value
// is greater than the
// divisor's value
if (arr[i] > arr[s])
{
dp[s] = Math.max(dp[i] + 1,
dp[s]);
}
}
else
{
// If current value is greater
// than the divisor's value
// and s is not equal
// to current index
if (s != i && arr[i] > arr[s])
dp[s] = Math.max(dp[i] + 1,
dp[s]);
// Condition if current
// value is greater
// than the divisor's value
if (arr[i] > arr[j])
{
dp[j] = Math.max(dp[i] + 1,
dp[j]);
}
}
}
}
}
int max = 0;
// Computing the greatest value
for(int i = 1; i < n; i++)
{
if (dp[i] > max)
max = dp[i];
}
// Printing maximum length of array
System.out.println(max);
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 0, 1, 4, 2, 3, 6, 4, 9 };
int size = arr.length;
// Function call
maxLength(arr, size);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
from math import *
# Function that print maximum length
# of array
def maxLength (arr, n):
# dp[] array to store the
# maximum length
dp = [1] * n
for i in range(n - 1, 1, -1):
# Find all divisors of i
for j in range(1, int(sqrt(i)) + 1):
if (i % j == 0):
s = i // j
if (s == j):
# If the current value
# is greater than the
# divisor's value
if (arr[i] > arr[s]):
dp[s] = max(dp[i] + 1, dp[s])
else:
# If current value
# is greater
# than the divisor's value
# and s is not equal
# to current index
if (s != i and arr[i] > arr[s]):
dp[s] = max(dp[i] + 1, dp[s])
# Condition if current
# value is greater
# than the divisor's value
if (arr[i] > arr[j]):
dp[j] = max(dp[i] + 1, dp[j])
Max = 0
# Computing the greatest value
for i in range(1, n):
if (dp[i] > Max):
Max = dp[i]
# Printing maximum length of array
print(Max)
# Driver Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 0, 1, 4, 2, 3, 6, 4, 9]
size = len(arr)
# Function call
maxLength(arr, size)
# This code is contributed by himanshu77
C#
// C# program for the above approach
using System;
class GFG{
// Function that print maximum length
// of array
static void maxLength(int[] arr, int n)
{
// dp[] array to store the
// maximum length
int[] dp = new int[n];
for(int i = 1; i < n; i++)
{
dp[i] = 1;
}
for(int i = n - 1; i > 1; i--)
{
// Find all divisors of i
for(int j = 1;
j <= Math.Sqrt(i); j++)
{
if (i % j == 0)
{
int s = i / j;
if (s == j)
{
// If the current value
// is greater than the
// divisor's value
if (arr[i] > arr[s])
{
dp[s] = Math.Max(dp[i] + 1,
dp[s]);
}
}
else
{
// If current value is greater
// than the divisor's value
// and s is not equal
// to current index
if (s != i && arr[i] > arr[s])
dp[s] = Math.Max(dp[i] + 1,
dp[s]);
// Condition if current
// value is greater
// than the divisor's value
if (arr[i] > arr[j])
{
dp[j] = Math.Max(dp[i] + 1,
dp[j]);
}
}
}
}
}
int max = 0;
// Computing the greatest value
for(int i = 1; i < n; i++)
{
if (dp[i] > max)
max = dp[i];
}
// Printing maximum length of array
Console.WriteLine(max);
}
// Driver Code
public static void Main()
{
// Given array arr[]
int[] arr = new int[] { 0, 1, 4, 2,
3, 6, 4, 9 };
int size = arr.Length;
// Function call
maxLength(arr, size);
}
}
// This code is contributed by sanjoy_62
Javascript
3
时间复杂度: O(N *(sqrt(N))由于既然对于数组的每个索引,我们都将计算其所有的除数,所以需要O(sqrt(N))
辅助空间: O(N)