RainX_69

Count Special Increasing Quadruplets (HARD PROBLEM) LEETCODE

Feb 4th, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 KB | Source Code | 0 0
  1. https://leetcode.com/problems/count-increasing-quadruplets/
  2.  
  3. Given a 0-indexed integer array nums of size n containing all numbers from 1 to n, return the number of increasing quadruplets.
  4.  
  5. A quadruplet (i, j, k, l) is increasing if:
  6.  
  7. 0 <= i < j < k < l < n, and
  8. nums[i] < nums[k] < nums[j] < nums[l].
  9.  
  10.  
  11. Example 1:
  12. Input: nums = [1,3,2,4,5]
  13. Output: 2
  14.  
  15. Explanation:
  16. - When i = 0, j = 1, k = 2, and l = 3, nums[i] < nums[k] < nums[j] < nums[l].
  17. - When i = 0, j = 1, k = 2, and l = 4, nums[i] < nums[k] < nums[j] < nums[l].
  18. There are no other quadruplets, so we return 2.
  19.  
  20. Example 2:
  21. Input: nums = [1,2,3,4]
  22. Output: 0
  23.  
  24. Explanation: There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] < nums[k], we return 0.
  25.  
  26.  
  27. Constraints:
  28.  
  29. 4 <= nums.length <= 4000
  30. 1 <= nums[i] <= nums.length
  31. All the integers of nums are unique. nums is a permutation.
  32.  
  33. ---------------------------------------------------------------------------------------------------------------------------------------
  34.  
  35. class Solution {
  36. public:
  37.     long long countQuadruplets(vector<int>& nums) {
  38.         int n=nums.size();
  39.         vector<vector<long long>> prefix(n+1,vector<long long>(n,0)); // finding count of elements greater than nums[i] from (i+1)->(n-1)
  40.         vector<vector<long long>> suffix(n+1,vector<long long>(n,0)); // finding count of elements smaller than nums[i] from 0->(i-1)
  41.        
  42.        
  43.         for(int i=0;i<n;i++){
  44.             int x=nums[i];
  45.             for(int j=i+1;j<n;j++){
  46.                 if(x<nums[j]){
  47.                     prefix[x][j]=1;
  48.                 }
  49.                 prefix[x][j]+=prefix[x][j-1];
  50.             }
  51.         }
  52.        
  53.         for(int i=n-1;i>=0;i--){
  54.             int x=nums[i];
  55.             for(int j=i-1;j>=0;j--){
  56.                 if(x>nums[j]){
  57.                     suffix[x][j]=1;
  58.                 }
  59.                 suffix[x][j]+=suffix[x][j+1];
  60.             }
  61.         }
  62.        
  63.         long long res=0;
  64.        
  65.         // here index variable i,j,k,l have exactly the same meaning as the question
  66.         for(int j=1;j<n-2;j++){
  67.             for(int k=j+1;k<n-1;k++){
  68.                 if(nums[k]<nums[j]){
  69.                     long long i=suffix[nums[k]][0]-suffix[nums[k]][j]; // finding count of elements smaller than nums[k] in index range (0,j-1)
  70.                     long long l=prefix[nums[j]][n-1]-prefix[nums[j]][k]; // finding count of elements greater than nums[j] in index range (k+1,n-1)
  71.                     res+=i*l;
  72.                 }
  73.             }
  74.         }
  75.         return res;
  76.     }
  77. };
  78.  
  79.  
  80. /*
  81. class Solution {
  82. private:
  83.     int tree[4*4000+10];
  84. public:
  85.     void update(int low, int high, int parent, int index){
  86.         if(low==high){
  87.             tree[parent]=1;
  88.             return;
  89.         }
  90.         int mid=(low+high)/2;
  91.         if(index<=mid){
  92.             update(low,mid,2*parent+1,index);
  93.         }
  94.         else{
  95.             update(mid+1,high,2*parent+2,index);
  96.         }
  97.         tree[parent]=tree[2*parent+1]+tree[2*parent+2];
  98.     }
  99.    
  100.     int query(int low, int high, int parent, int lowerR, int higherR){
  101.         if(low>higherR || high<lowerR){
  102.             return 0;
  103.         }
  104.         if(low>=lowerR && high<=higherR){
  105.             return tree[parent];
  106.         }
  107.         int mid=(low+high)/2;
  108.         return query(low,mid,2*parent+1,lowerR,higherR)+query(mid+1,high,2*parent+2,lowerR,higherR);
  109.     }
  110.    
  111.     long long countQuadruplets(vector<int>& nums) {
  112.         long long res=0;
  113.         int n=nums.size();
  114.         vector<vector<int>> precompute(n+1,vector<int>(n,0));
  115.         vector<vector<int>> smallerThan(n+1);
  116.        
  117.         for(int i=0;i<n;i++){
  118.             int x=nums[i];
  119.             for(int j=i+1;j<n;j++){
  120.                 if(x<nums[j]){
  121.                     precompute[x][j]=1;
  122.                 }
  123.                 if(x>nums[j] && j<n-1){
  124.                     smallerThan[x].push_back(j);
  125.                 }
  126.                 precompute[x][j]+=precompute[x][j-1];
  127.             }
  128.         }
  129.        
  130.         for(int j=0;j<n-2;j++){
  131.             int x=nums[j];
  132.             vector<int> K=smallerThan[x];
  133.             for(auto k: K){
  134.                 long long lessThank=query(0,n-1,0,0,nums[k]-1);
  135.                 long long greaterThanX=precompute[x][n-1]-precompute[x][k];
  136.                 res+=greaterThanX*lessThank;
  137.             }
  138.             update(0,n-1,0,x);
  139.         }
  140.         return res;
  141.     }
  142. };
  143. */ // segment tree but kinda useless actually. I OVER-thought it and its really not that useful
Advertisement
Add Comment
Please, Sign In to add comment