Advertisement
knakul853

Untitled

Jul 24th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int lengthOfLIS(vector<int>& nums) {
  4.         int n = nums.size();
  5.         if(n==0)return 0;
  6.         vector<int>ans;
  7.        for(int i=0;i<n;i++)
  8.        {
  9.            auto it = std::lower_bound(ans.begin(),ans.end(),nums[i]);
  10.            if(it == ans.end())ans.push_back(nums[i]);
  11.            else *it = nums[i];
  12.        }
  13.         return ans.size();
  14.        
  15.        
  16.        
  17.     }
  18. };
  19.  
  20. //n^2
  21. class Solution {
  22. public:
  23.     int lengthOfLIS(vector<int>& nums) {
  24.         int n = nums.size();
  25.         if(n==0)return 0;
  26.        
  27.         vector<int>dp(n+1,1);
  28.        
  29.         for(int i=1;i<n;i++)
  30.         {
  31.             for(int j=0;j<i;j++)
  32.             {
  33.                 if(nums[i] > nums[j] && dp[j]+1 > dp[i])
  34.                 {
  35.                     dp[i] = dp[j]+1;
  36.                 }
  37.             }
  38.         }
  39.        
  40.         return *max_element(dp.begin(),dp.end());
  41.        
  42.        
  43.     }
  44. };
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement