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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter