给定一个由N 个整数组成的数组arr[] ,任务是找到并打印最长递增子序列。
例子:
Input: arr[] = {12, 34, 1, 5, 40, 80}
Output: 4
{12, 34, 40, 80} and {1, 5, 40, 80} are the
longest increasing subsequences.
Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60, 80}
Output: 6
先决条件:LCS,LIS
方法:任何序列的最长递增子序列是其已排序序列的子序列。它可以使用动态规划方法解决。该方法与经典 LCS 问题相同,但不是第二个序列,而是再次以其排序形式采用给定序列。
注意:数组应该有不同的元素,否则可能会给出错误的结果。例如,在 {1, 1, 1} 中,我们知道最长递增子序列 (a1 < a2 < … ak) 的长度为 1,但是如果我们在 LIS 中使用 LCS 方法尝试这个示例,我们将得到 3(因为它找到了最长公共子序列)。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the size of the
// longest increasing subsequence
int LISusingLCS(vector& seq)
{
int n = seq.size();
// Create an 2D array of integer
// for tabulation
vector > L(n + 1, vector(n + 1));
// Take the second sequence as the sorted
// sequence of the given sequence
vector sortedseq(seq);
sort(sortedseq.begin(), sortedseq.end());
// Classical Dynamic Programming algorithm
// for Longest Common Subsequence
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (seq[i - 1] == sortedseq[j - 1])
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
// Return the ans
return L[n][n];
}
// Driver code
int main()
{
vector sequence{ 12, 34, 1, 5, 40, 80 };
cout << LISusingLCS(sequence) << endl;
return 0;
}
Java
//Java implementation of above approach
import java.util.*;
class GFG
{
// Function to return the size of the
// longest increasing subsequence
static int LISusingLCS(Vector seq)
{
int n = seq.size();
// Create an 2D array of integer
// for tabulation
int L[][] = new int [n + 1][n + 1];
// Take the second sequence as the sorted
// sequence of the given sequence
Vector sortedseq = new Vector(seq);
Collections.sort(sortedseq);
// Classical Dynamic Programming algorithm
// for Longest Common Subsequence
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (seq.get(i - 1) == sortedseq.get(j - 1))
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = Math.max(L[i - 1][j],
L[i][j - 1]);
}
}
// Return the ans
return L[n][n];
}
// Driver code
public static void main(String args[])
{
Vector sequence = new Vector();
sequence.add(12);
sequence.add(34);
sequence.add(1);
sequence.add(5);
sequence.add(40);
sequence.add(80);
System.out.println(LISusingLCS(sequence));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
# Function to return the size of the
# longest increasing subsequence
def LISusingLCS(seq):
n = len(seq)
# Create an 2D array of integer
# for tabulation
L = [[0 for i in range(n + 1)]
for i in range(n + 1)]
# Take the second sequence as the sorted
# sequence of the given sequence
sortedseq = sorted(seq)
# Classical Dynamic Programming algorithm
# for Longest Common Subsequence
for i in range(n + 1):
for j in range(n + 1):
if (i == 0 or j == 0):
L[i][j] = 0
elif (seq[i - 1] == sortedseq[j - 1]):
L[i][j] = L[i - 1][j - 1] + 1
else:
L[i][j] = max(L[i - 1][j],
L[i][j - 1])
# Return the ans
return L[n][n]
# Driver code
sequence = [12, 34, 1, 5, 40, 80]
print(LISusingLCS(sequence))
# This code is contributed by mohit kumar
C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the size of the
// longest increasing subsequence
static int LISusingLCS(List seq)
{
int n = seq.Count;
// Create an 2D array of integer
// for tabulation
int [,]L = new int [n + 1, n + 1];
// Take the second sequence as the sorted
// sequence of the given sequence
List sortedseq = new List(seq);
sortedseq.Sort();
// Classical Dynamic Programming algorithm
// for longest Common Subsequence
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
L[i, j] = 0;
else if (seq[i - 1] == sortedseq[j - 1])
L[i, j] = L[i - 1, j - 1] + 1;
else
L[i,j] = Math.Max(L[i - 1, j],
L[i, j - 1]);
}
}
// Return the ans
return L[n, n];
}
// Driver code
public static void Main(String []args)
{
List sequence = new List();
sequence.Add(12);
sequence.Add(34);
sequence.Add(1);
sequence.Add(5);
sequence.Add(40);
sequence.Add(80);
Console.WriteLine(LISusingLCS(sequence));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
4
时间复杂度: O(n 2 ) 其中 n 是序列的长度
辅助空间: O(n 2 )