Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a7d9c8

Browse files
Merge pull request #119 from lucianosz7/LIS-ALGORITHM
Adding Longest Increasing Subsequence algorithm into Dynamic Programming
2 parents 0aaf8c5 + 3a74c45 commit 9a7d9c8

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /