Advertisement
maycod23

Untitled

Jun 9th, 2022
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 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. }
  78.  
  79.  
  80.  
  81. ///////////////////////////////////reverse a stack through recursion
  82.  
  83. class Solution{
  84. public:
  85.     void insertatend(stack<int>& s,int ele){
  86.         if(s.size()==0){
  87.             s.push(ele); return;
  88.         }
  89.        
  90.         int temp=s.top(); s.pop();
  91.         insertatend(s,ele);
  92.         s.push(temp);
  93.     }
  94.    
  95.     void reverse(stack<int> &s){
  96.         if(s.size()==1) return;
  97.        
  98.         int temp=s.top(); s.pop();
  99.         reverse(s);
  100.         insertatend(s,temp);
  101.     }
  102.    
  103.     vector<int> Reverse(stack<int> st){
  104.         // 1      4
  105.         // 2      3    
  106.         // 3      2      
  107.         // 4......1  
  108.         int n=st.size(),i;
  109.         vector<int> ans(n,0);
  110.         if(st.size()==0) return ans;
  111.         reverse(st);
  112.         i=(n-1);
  113.          while(!st.empty()){
  114.             ans[i]=st.top(); i--; st.pop();
  115.         }
  116.              
  117.         return ans;
  118.     }
  119. };
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement