Skip to content

Commit 8945b7c

Browse files
committed
added longest increasing subsequence code
1 parent 4e86f64 commit 8945b7c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Longest Increasing Subsequence
2+
#include<iostream>
3+
using namespace std;
4+
int lis(int* a,int n)
5+
{
6+
int* output=new int[n];
7+
output[0]=1;
8+
for(int i=1;i<n;i++)
9+
{
10+
output[i]=1;
11+
for(int j=i-1;j>=0;j--)
12+
{
13+
if(a[j]<a[i] && output[i]<output[j]+1)
14+
{
15+
output[i]=output[j]+1;
16+
}
17+
}
18+
}
19+
int ans=0;
20+
for(int i=0;i<n;i++)
21+
{
22+
if(ans<output[i])
23+
{
24+
ans=output[i];
25+
}
26+
}
27+
return ans;
28+
29+
}
30+
int main()
31+
{
32+
int size;
33+
cin>>size;
34+
int* arr=new int[size];
35+
for(int i=0;i<size;i++)
36+
{
37+
cin>>arr[i];
38+
}
39+
cout<<lis(arr,size);
40+
return 0;
41+
}

0 commit comments

Comments
 (0)