Stock span problem

PHOTO EMBED

Sun Dec 17 2023 05:15:27 GMT+0000 (Coordinated Universal Time)

Saved by @nistha_jnn #c++

  vector <int> calculateSpan(int price[], int n)
    {
       vector<int>ans;
       stack<pair<int,int>>st;
       for(int i=0 ; i<n ; i++)
       {
           
           while(!st.empty() and st.top().first<=price[i])
           {
               st.pop();
           }
           if(st.empty())
           {
               ans.push_back(-1);
           }
           else if(!st.empty() and st.top().first>price[i])
           {
                ans.push_back(st.top().second);
           }
        st.push({price[i],i});
       }
       for(int i=0 ; i<n ; i++)
       {
           ans[i]=i-ans[i];
       }
       return ans;
    }
content_copyCOPY