## 901. Online Stock Span https://leetcode.com/problems/online-stock-span/description/ ## Monotonic stack - Complexity - Time: O(1) for amortized analysis - Space: O(N) in worst case if it's decreasing ```= class StockSpanner { stack<pair<int, int>> stk; // { price, span } public: StockSpanner() { } int next(int price) { int span = 1; while(!stk.empty() and price >= stk.top().first){ span += stk.top().second; stk.pop(); } stk.push({price, span}); return span; } }; /** * Your StockSpanner object will be instantiated and called as such: * StockSpanner* obj = new StockSpanner(); * int param_1 = obj->next(price); */ ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up