Advertisement
29rohitkr

Stock Span problem GFG

Feb 22nd, 2023
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | Source Code | 0 0
  1. vector <int> calculateSpan(int price[], int n)
  2.     {
  3.        // Your code here
  4.        vector<int> res;
  5.        stack<int> st;
  6.        res.push_back(1);
  7.        st.push(0);
  8.        for(int i=1; i<n; i++)
  9.        {
  10.            while(st.empty()==false && price[st.top()] <= price[i] )
  11.            {
  12.                st.pop();
  13.            }
  14.            if(st.empty()){
  15.                res.push_back(i+1);
  16.            }
  17.            else
  18.            {
  19.                res.push_back(i-st.top());
  20.            }
  21.            st.push(i);
  22.        }
  23.        return res;
  24.     }
Tags: dsa gfg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement