Longest Increasing Subsequence-dynamic programming
Given a sequence we should find the length of the longest increasing subsequence .
example:
n=7
a[n]={3,4,-1,0,6,2,3}
the longest increasing subsequence is -1,0,2,3 and its length is 4
so output=>4
lets use dynamic programming to solve this question:
c++ implementation:
t is the no.of test cases:
example:
n=7
a[n]={3,4,-1,0,6,2,3}
the longest increasing subsequence is -1,0,2,3 and its length is 4
so output=>4
lets use dynamic programming to solve this question:
c++ implementation:
t is the no.of test cases:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void lss(int a[],int n)
{ int dp[n];
for(int i=0;i<n;i++)
dp[i]=1;
for(int i=1;i<n;i++)
{
for(int j=0;j<i;j++)
{
if(a[i]>a[j])
dp[i]=max(dp[i],dp[j]+1);
}
}
int maxele=dp[0];
for(int i=1;i<n;i++)
maxele=max(maxele,dp[i]);
cout<<maxele;
}
int main() { int t; cin>>t;
while(t--)
{
int n; cin>>n;
int a[n];int dp[n];
for(int i=0;i<n;i++)
cin>>a[i];
lss(a,n);
cout<<endl;
}
//code
return 0;
}