You are viewing a single comment's thread. Return to all comments →
Stack
long stockmax(vector<int> prices) { long sum=0; stack<pair<int,int>>st; for(int i=prices.size()-1;i>-1;i--) if(st.empty()||prices[i]>st.top().first) st.push({prices[i],i}); for(int i=0;i<prices.size();i++){ if(i<st.top().second&&prices[i]<st.top().first)sum+=(st.top().first-prices[i]); else if(i==st.top().second)st.pop(); } return sum; }
Seems like cookies are disabled on this browser, please enable them to open this website
Stock Maximize
You are viewing a single comment's thread. Return to all comments →
Stack