Advertisement
maycod23

Untitled

Jun 7th, 2022
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. ////////////////////////////////maximum area of good subarray
  2.  
  3.  
  4. class Solution {
  5. public:
  6.     int maximumScore(vector<int>& a, int k)
  7.     {
  8.       //so here what we are  doing is basically is
  9.         //intuition -> each element can be  minima at some point
  10.         //considering minima as each element and finding its left and right
  11.         //extreme points where it is lying basically.
  12.         int n=a.size();
  13.         stack<pair<int,int>> stk;
  14.         vector<int> width(n);
  15.         for(int i=(n-1);i>=0;i--)
  16.         {
  17.             //for each a[i] we have to find a x in right to a[i]
  18.             //which is just smaller than a[i], because then a[i] will be
  19.             // minima from i to x-1;
  20.             while(stk.size()>0&&stk.top().first>=a[i]) stk.pop();
  21.             if(stk.empty())
  22.             {
  23.                 width[i]=(n-1);//no element present in right smaller than i
  24.                 //so ithe element is minima from (i to (n-1))
  25.             }
  26.             else
  27.             {
  28.                 //currently at smaller element than a[i];
  29.                 width[i]=stk.top().second-1;
  30.             }
  31.             stk.push(make_pair(a[i],i));
  32.         }
  33.         while(!stk.empty()) stk.pop();
  34.         for(int i=0;i<=(n-1);i++)
  35.         {
  36.             while(stk.size()>0&&stk.top().first>=a[i]) stk.pop();
  37.             if(stk.empty())
  38.             {
  39.                 int left=0,right=width[i];//is is minima from (i to 0)
  40.                 if(left<=k&&k<=right) width[i]=(right-left+1);
  41.                 else width[i]=0;
  42.             }
  43.             else
  44.             {
  45.                int left=stk.top().second+1,right=width[i];
  46.                 //is is minima from (i to left)
  47.                if(left<=k&&k<=right) width[i]=(right-left+1);
  48.                else width[i]=0;
  49.             }
  50.             stk.push(make_pair(a[i],i));
  51.         }
  52.         int ans=-1;
  53.         for(int i=0;i<=(n-1);i++) ans=max(ans,(a[i]*width[i]));
  54.         return ans;
  55.     }
  56. };
  57.  
  58.  
  59.  
  60. ////////////////////////////////////////////////////sort a stack using recursion
  61. void SortedStack :: sort()
  62. {
  63.    //Your code here
  64.    //this has access to stack and you have to just sort it
  65.    //top to bottom like
  66.    //11 2 32 3 41
  67.    if(s.size()==1||s.size()==0) return;
  68.    int temp=s.top(); s.pop();
  69.    sort();
  70.    if(temp>=s.top()){
  71.        s.push(temp); return;
  72.    }
  73.    int temp2=s.top(); s.pop();
  74.    s.push(temp);
  75.    sort();
  76.    s.push(temp2);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement