forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIS.cpp
38 lines (28 loc) · 913 Bytes
/
LIS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;
int lis(vector<int> const arr)
{
int n=arr.size();
// array that contains the longest increasing subsequence until element i
vector<int> lis(arr.size());
for (int i=0; i < n; ++i) {
lis[i]=1;
}
int ans=-1<<30; // length of the lis starts with -infinite
for (int i=1;i<n;++i) {
for (int j=0; j < i; ++j) {
if (arr[j]<arr[i]) {
lis[i]=max(lis[i], lis[j]+1);
ans=max(ans, lis[i]);
}
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
// arr = array of elements
vector<int> arr{10, 22, 9, 33, 21, 50, 41, 60};
cout << "Size of the longest increasing subsequence: " << lis(arr) << "\n";
}