给定一个由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)