class Solution {
private:
int fun(int ind,int prev,int n,vector& nums){
if(ind==n) return 0;
code
int len= 0+fun(ind+1,prev,n,nums); //not take
if(prev ==-1 || nums[ind]>nums[prev]){ //take
len= max(len,1+ fun(ind+1,ind,n,nums));
}
return = len;
}
public:
int lengthOfLIS(vector& nums) {
int n= nums.size();
return fun(0,-1,n,nums);
}
};
If don't want length I want to print from this lis
Assistant
To modify your existing code to print the actual Longest Increasing Subsequence (LIS) instead of just returning its length, you'll need to adjust the approach to keep track of the elements in the LIS as you compute it.
Here's a step-by-step guide to achieve this:
Modify the Recursive Function to Return the LIS:
Instead of returning just the length of the LIS, have the recursive function return the actual subsequence as a vector. This involves choosing between taking or not taking the current element and building the subsequence accordingly.
Use Memoization to Optimize:
To avoid redundant computations, especially since recursive calls can lead to exponential time complexity, use memoization. You can store the results of subproblems in a memoization table.
Implement the Function to Reconstruct the LIS:
After computing the LIS length, you'll need to backtrack through the decisions made during recursion to reconstruct the actual subsequence.
Below is the modified C++ code incorporating these ideas:
cpp
#include <bits/stdc++.h>
using namespace std;
class Solution {
private:
// Memoization table: dp[ind][prev+1] will store the LIS from index 'ind' with previous index 'prev'
// Note: 'prev' can be -1, so we offset it by +1
vector<vector<vector<int>>> dpMemo;
// Recursive function to find the LIS starting from 'ind' with previous element at 'prev'
vector<int> fun(int ind, int prev, int n, vector<int>& nums) {
if(ind == n) return {}; // Base case: empty subsequence
// Check if already computed
if(dpMemo[ind][prev + 1].size() > 0){
return dpMemo[ind][prev + 1];
}
// Option 1: Do not take nums[ind]
vector<int> notTake = fun(ind + 1, prev, n, nums);
// Option 2: Take nums[ind] if it's greater than the previous element
vector<int> take;
if(prev == -1 || nums[ind] > nums[prev]){
take = fun(ind + 1, ind, n, nums);
take.insert(take.begin(), nums[ind]); // Include current element
}
// Choose the longer subsequence
if(take.size() > notTake.size()){
dpMemo[ind][prev + 1] = take;
}
else{
dpMemo[ind][prev + 1] = notTake;
}
return dpMemo[ind][prev + 1];
}
public:
vector<int> getLIS(vector<int>& nums) {
int n = nums.size();
// Initialize memoization table with empty vectors
dpMemo = vector<vector<vector<int>>>(n, vector<vector<int>>(n + 1, vector<int>()));
return fun(0, -1, n, nums);
}
};
int main(){
Solution sol;
vector<int> nums = {10,9,2,5,3,7,101,18};
vector<int> lis = sol.getLIS(nums);
cout << "Length of LIS: " << lis.size() << "\nLIS: ";
for(auto num : lis){
cout << num << " ";
}
return 0;
}
Explanation of the Modified Code:
Memoization Table (dpMemo):
A 3D vector is used where dpMemo[ind][prev + 1] stores the LIS starting from index ind with the previous element at index prev.
The +1 offset accounts for the case when prev = -1 (i.e., no previous element).
Recursive Function (fun):
Base Case: If ind == n, return an empty vector since there are no more elements to consider.
Not Take Option: Recursively call fun without including nums[ind].
Take Option: If nums[ind] is greater than the previous element (nums[prev]), include it in the subsequence and recursively call fun for the next index.
Choose the Longer Subsequence: Between the "take" and "not take" options, choose the one that yields the longer subsequence and store it in dpMemo.
Driver Code (main Function):
An example array nums is provided.
getLIS computes the LIS.
The length and the actual LIS are printed.
Sample Output:
code
Length of LIS: 4
LIS: 2 3 7 101
Notes:
Time and Space Complexity:
The above recursive solution with memoization ensures that each subproblem is solved only once. However, since we're storing actual subsequences, the space complexity can be high for large input sizes.
Iterative DP Approach:
For larger inputs or for better performance, consider using an iterative Dynamic Programming approach with backtracking to reconstruct the LIS. This typically has better space efficiency.
Alternative Solutions:
The patience sorting algorithm can find the length of the LIS in O(n log n) time, and with additional bookkeeping, you can reconstruct the subsequence as well. However, it's more complex to implement.
Feel free to adjust the code based on your specific requirements or constraints. If you have any further questions or need clarifications, feel free to ask!