901. 股票价格跨度
程序员文章站
2022-03-27 20:43:52
...
方法:单调栈
class StockSpanner {
public:
stack<int> st;
vector<int> prices;
vector<int> res;
StockSpanner() {
}
int next(int price) {
prices.push_back(price);
int ret = prices.size() - 1;
while(!st.empty() && price>=prices[st.top()]){
ret = res[st.top()];
st.pop();
}
st.push(prices.size()-1);
res.push_back(ret);
return prices.size() - ret;
}
};
上一篇: 巧用TypeScript