knakul853

Untitled

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