给定一个大小为N的数组arr[] ,任务是找到最长的递增子序列,使得任何元素的索引都可以被前一个元素的索引 (LIIDS) 整除。以下是LIIDS的必要条件:
如果 i, j 是给定数组中的两个索引。然后:
- 我 < j
- j % i = 0
- arr[i] < arr[j]
例子:
Input: arr[] = {1, 2, 3, 7, 9, 10}
Output: 3
Explanation:
The subsequence = {1, 2, 7}
Indices of the elements of sub-sequence = {1, 2, 4}
Indices condition:
2 is divisible by 1
4 is divisible by 2
OR
Another possible sub-sequence = {1, 3, 10}
Indices of elements of sub-sequence = {1, 3, 6}
Indices condition:
3 is divisible by 1
6 is divisible by 3
Input: arr[] = {7, 1, 3, 4, 6}
Output: 2
Explanation:
The sub-sequence = {1, 4}
Indices of elements of sub-sequence = {2, 4}
Indices condition:
4 is divisible by 2
方法:思路是利用动态规划的概念来解决这个问题。
- 首先创建一个 dp[] 数组,并用 1 初始化该数组,这是因为除法子序列的最小长度为 1。
- 现在,对于数组中的每个索引 ‘i’,增加索引处所有倍数的值的计数。
- 最后,当对所有值执行上述步骤时,数组中存在的最大值就是所需的答案。
下面是上述方法的实现:
C++
// C++ program to find the length of
// the longest increasing sub-sequence
// such that the index of the element is
// divisible by index of previous element
#include
using namespace std;
// Function to find the length of
// the longest increasing sub-sequence
// such that the index of the element is
// divisible by index of previous element
int LIIDS(int arr[], int N)
{
// Initialize the dp[] array with 1 as a
// single element will be of 1 length
int dp[N + 1];
int ans = 0;
for (int i = 1; i <= N; i++) {
dp[i] = 1;
}
// Traverse the given array
for (int i = 1; i <= N; i++) {
// If the index is divisible by
// the previous index
for (int j = i + i; j <= N; j += i) {
// if increasing
// subsequence identified
if (arr[j] > arr[i]) {
dp[j] = max(dp[j], dp[i] + 1);
}
}
// Longest length is stored
ans = max(ans, dp[i]);
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 7, 9, 10 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << LIIDS(arr, N);
return 0;
}
Java
// Java program to find the length of
// the longest increasing sub-sequence
// such that the index of the element is
// divisible by index of previous element
import java.util.*;
class GFG{
// Function to find the length of
// the longest increasing sub-sequence
// such that the index of the element is
// divisible by index of previous element
static int LIIDS(int arr[], int N)
{
// Initialize the dp[] array with 1 as a
// single element will be of 1 length
int[] dp = new int[N + 1];
int ans = 0;
for(int i = 1; i <= N; i++)
{
dp[i] = 1;
}
// Traverse the given array
for(int i = 1; i <= N; i++)
{
// If the index is divisible by
// the previous index
for(int j = i + i; j <= N; j += i)
{
// If increasing
// subsequence identified
if (j < arr.length && arr[j] > arr[i])
{
dp[j] = Math.max(dp[j], dp[i] + 1);
}
}
// Longest length is stored
ans = Math.max(ans, dp[i]);
}
return ans;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 2, 3, 7, 9, 10 };
int N = arr.length;
System.out.println(LIIDS(arr, N));
}
}
// This code is contributed by AbhiThakur
Python3
# Python3 program to find the length of
# the longest increasing sub-sequence
# such that the index of the element is
# divisible by index of previous element
# Function to find the length of
# the longest increasing sub-sequence
# such that the index of the element is
# divisible by index of previous element
def LIIDS(arr, N):
# Initialize the dp[] array with 1 as a
# single element will be of 1 length
dp = []
for i in range(1, N + 1):
dp.append(1)
ans = 0
# Traverse the given array
for i in range(1, N + 1):
# If the index is divisible by
# the previous index
j = i + i
while j <= N:
# If increasing
# subsequence identified
if j < N and i < N and arr[j] > arr[i]:
dp[j] = max(dp[j], dp[i] + 1)
j += i
# Longest length is stored
if i < N:
ans = max(ans, dp[i])
return ans
# Driver code
arr = [ 1, 2, 3, 7, 9, 10 ]
N = len(arr)
print(LIIDS(arr, N))
# This code is contributed by ishayadav181
C#
// C# program to find the length of
// the longest increasing sub-sequence
// such that the index of the element is
// divisible by index of previous element
using System;
class GFG{
// Function to find the length of
// the longest increasing sub-sequence
// such that the index of the element is
// divisible by index of previous element
static int LIIDS(int[] arr, int N)
{
// Initialize the dp[] array with 1 as a
// single element will be of 1 length
int[] dp = new int[N + 1];
int ans = 0;
for(int i = 1; i <= N; i++)
{
dp[i] = 1;
}
// Traverse the given array
for(int i = 1; i <= N; i++)
{
// If the index is divisible by
// the previous index
for(int j = i + i; j <= N; j += i)
{
// If increasing
// subsequence identified
if (j < arr.Length && arr[j] > arr[i])
{
dp[j] = Math.Max(dp[j], dp[i] + 1);
}
}
// Longest length is stored
ans = Math.Max(ans, dp[i]);
}
return ans;
}
// Driver code
public static void Main()
{
int[] arr = new int[] { 1, 2, 3, 7, 9, 10 };
int N = arr.Length;
Console.WriteLine(LIIDS(arr, N));
}
}
// This code is contributed by sanjoy_62
Javascript
3
时间复杂度: O(N log(N)) ,其中 N 是数组的长度。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live