Advertisement
nikunjsoni

673

Jun 14th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int findNumberOfLIS(vector<int>& nums) {
  4.         int n = nums.size();
  5.         vector<int> len(n, 1);  
  6.         vector<int> count(n, 1);
  7.         int maxLen = 1;
  8.        
  9.         // O(N^2) DP Solution
  10.         for(int i=1; i<n;i++){
  11.             for(int j=0; j<i; j++){
  12.                 if(nums[i] > nums[j]){ // strictly increasing
  13.                     if(len[j]+1 > len[i]){
  14.                         len[i] = len[j] + 1;
  15.                         count[i] = count[j];
  16.                     }
  17.                     else if(len[j]+1 == len[i]){
  18.                         count[i] += count[j];
  19.                     }
  20.                 }
  21.             }
  22.             maxLen = max(maxLen, len[i]);
  23.         }
  24.        
  25.         // Count all subsequences of maxlen
  26.         int ans = 0;
  27.         for(int i=0; i<n; i++)
  28.             if(len[i] == maxLen)
  29.                 ans += count[i];
  30.            
  31.         return ans;
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement