1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ /*
5
+ The longest increasing subsequence problem is to find a subsequence of a given sequence in which
6
+ he subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is
7
+ as long as possible.
8
+ */
9
+
10
+ int _lis (int arr[], int n, int * max_ref)
11
+ {
12
+
13
+ if (n == 1 )
14
+ return 1 ;
15
+
16
+ int res, max_ending_here = 1 ;
17
+
18
+ for (int i = 1 ; i < n; i++)
19
+ {
20
+ res = _lis (arr, i, max_ref);
21
+ if (arr[i - 1 ] < arr[n - 1 ] &&
22
+ res + 1 > max_ending_here)
23
+ max_ending_here = res + 1 ;
24
+ }
25
+
26
+ if (*max_ref < max_ending_here)
27
+ *max_ref = max_ending_here;
28
+
29
+ return max_ending_here;
30
+ }
31
+
32
+ int lis (int arr[], int n)
33
+ {
34
+
35
+ int max = 1 ;
36
+
37
+ _lis (arr, n, &max);
38
+
39
+ return max;
40
+ }
41
+
42
+ int main ()
43
+ {
44
+ int arr[] = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 };
45
+ int n = sizeof (arr) / sizeof (arr[0 ]);
46
+
47
+ cout << " Length of lis is " << lis (arr, n) << " \n " ;
48
+ return 0 ;
49
+ }
0 commit comments