jayati

LIS DP TLE

Oct 28th, 2023
1,389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // } Driver Code Ends
  6.  
  7.  
  8. class Solution
  9. {
  10.     public:
  11.     //Function to find length of longest increasing subsequence.
  12.     int longestSubsequence(int n, int a[])
  13.     {
  14.         vector<int> dp(n, 1);
  15.         int maxi = 1;
  16.         for (int i = 0; i < n; i++)
  17.         {
  18.             for (int j = 0; j < i; j++)
  19.             {
  20.                 if (a[j] < a[i] && 1 + dp[j] > dp[i])
  21.                 {
  22.                     dp[i] = 1 + dp[j];
  23.                 }
  24.             }
  25.             maxi = max(maxi, dp[i]);
  26.         }
  27.         return maxi;
  28.        
  29.     }
  30. };
  31.  
  32. //{ Driver Code Starts.
  33. int main()
  34. {
  35.     //taking total testcases
  36.     int t,n;
  37.     cin>>t;
  38.     while(t--)
  39.     {
  40.         //taking size of array
  41.         cin>>n;
  42.         int a[n];
  43.        
  44.         //inserting elements to the array
  45.         for(int i=0;i<n;i++)
  46.             cin>>a[i];
  47.         Solution ob;
  48.         //calling method longestSubsequence()
  49.         cout << ob.longestSubsequence(n, a) << endl;
  50.     }
  51. }
  52.  
  53. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment