forked from ccgcv/Cplus-plus-for-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamic programming
55 lines (37 loc) · 965 Bytes
/
Dynamic programming
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
50
51
52
53
54
55
#include <iostream>
#include <string.h>
using namespace std;
int ceilIdx(int tail[], int l, int r, int key)
{
while (r > l) {
int m = l + (r - l) / 2;
if (tail[m] >= key)
r = m;
else
l = m+1;
}
return r;
}
int LIS(int arr[], int n)
{
int tail[n];
int len =1;
tail[0] = arr[0];
for (int i = 1; i < n; i++) {
if(arr[i] > tail[len - 1])
{
tail[len] = arr[i];
len++;
}
else{
int c = ceilIdx(tail, 0, len - 1, arr[i]);
tail[c] = arr[i];
}
}
return len;
}
int main() {
int arr[] ={3, 10, 20, 4, 6, 7};
int n = 6;
cout<<LIS(arr, n);
}