-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.nlogn.cpp
More file actions
49 lines (42 loc) · 764 Bytes
/
Copy pathLIS.nlogn.cpp
File metadata and controls
49 lines (42 loc) · 764 Bytes
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
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<algorithm>
using namespace std;
int search(int arr[], int beg, int end, int key)
{
int mid;
while(end-beg>1)
{
mid = (end+beg)/2;
(arr[mid]>=key ? end : beg) = mid;
}
return end;
}
int solve(int arr[], int n)
{
int temp[n];
temp[0]=arr[0];
int len=1;
for(int i=1;i<n;i++)
{
if(arr[i] < temp[0])
temp[0] = arr[i];
else if(arr[i] > temp[len-1])
{
temp[len++]=arr[i];
} else
{
temp[search(temp, -1, len-1, arr[i])] = arr[i];
}
}
int ret = 1;
for(int i=0;i<n;i++)
ret = max(ret, temp[i]);
return ret;
}
int main()
{
int A[] = { 2, 5, 3, 7, 11, 8, 10, 13, 6 };
int n = sizeof(A)/sizeof(A[0]);
printf("Length of Longest Increasing Subsequence is %d\n",solve(A, n));
return 0;
}