Skip to content

Commit

Permalink
Merge pull request #167 from nikhilpuria/master
Browse files Browse the repository at this point in the history
Adding Longest Increasing Subsequence DP algorithm in C++
  • Loading branch information
i-vishi authored Oct 6, 2018
2 parents 65fbc4c + fd1b3d2 commit 6232131
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* The program finds the length of the Longest subsequence (may not be
* continuous) such that the subsequence is in increasing order
*/
#include <bits/stdc++.h>
using namespace std;



int findMax(int arr[], int length) {
int max = 0;
for (int i = 0; i < length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}

int findLongestIncSubLength(int arr[],int length) {
int dp[length];
int i, j, max = 0;

// Initialize Longest Increasing Subsequence values
for (i = 0; i < length; i++) {
dp[i] = 1;
}

for (i = 1; i < length; i++) {
for (j = 0; j < i; j++) {
if (arr[i] > arr[j] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
}
}
}
max = findMax(dp, length);
return max;
}

int main() {
int arr[] = { 1, 4, 2, 10, 8 };
int lisLength = findLongestIncSubLength(arr,5);
cout << "Longest Increasing Subsequence Length is : " << lisLength << endl;

}

0 comments on commit 6232131

Please sign in to comment.