Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions algorithms/dynamic-programming/longest_increasing_subsequence.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
using namespace std;
int longestIncreasingSubsequence(int A[],int size){
int longestIncreasingSubsequenceThroughTabulation(int A[],int size){
int dp[size];
for(int i=0;i<size;i++) dp[i] = 1;
for(int i=1;i<size;i++){
Expand All @@ -17,9 +18,30 @@ int longestIncreasingSubsequence(int A[],int size){

return lis;
}
int longestIncreasingSubsequenceThroughMemoization(int A[],int idx,int prev,int size,vector<vector<int>>&dp){
if(idx>=size)
return 0;
if(dp[idx][prev+1]!=-1){
return dp[idx][prev+1];
}
int choise1=0;
int choise2=0;
if(prev==-1){
choise1=1+longestIncreasingSubsequenceThroughMemoization(A,idx+1,idx,size);
}
if(A[prev]<A[idx]){
choise1=1+longestIncreasingSubsequenceThroughMemoization(A,idx+1,idx,size);
}
choise2=longestIncreasingSubsequenceThroughMemoization(A,idx+1,prev,size);
return dp[idx][prev+1]=max(choise1,choise2);
}
int main() {

int A[] = {1,3,5,9,8};
cout<<longestIncreasingSubsequence(A,5);
cout<<"Tabulation -> "<<endl;
cout<<longestIncreasingSubsequenceThroughTabulation(A,5)<<endl;
vector<vector<int>>dp(size,vector<int>(size,-1));
cout<<"Memoization -> "<<endl;
cout<<longestIncreasingSubsequenceThroughMemoization(A,0,-1,5,dp)<<endl;
return 0;
}