2023每日刷題(五十二)
Leetcode—901.股票價格跨度
算法思想
實現代碼
class StockSpanner {
public:stack<pair<int, int>> st;int curday = -1;StockSpanner() {st.emplace(-1, INT_MAX);}int next(int price) {while(price >= st.top().second) {st.pop();}int ans = ++curday - st.top().first;st.emplace(curday, price);return ans;}
};/*** Your StockSpanner object will be instantiated and called as such:* StockSpanner* obj = new StockSpanner();* int param_1 = obj->next(price);*/
運行結果
之后我會持續更新,如果喜歡我的文章,請記得一鍵三連哦,點贊關注收藏,你的每一個贊每一份關注每一次收藏都將是我前進路上的無限動力 !!!↖(▔▽▔)↗感謝支持!