Skip to content

Commit cfef44b

Browse files
authored
Create online-stock-span.cpp
1 parent 8e33c39 commit cfef44b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

C++/online-stock-span.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class StockSpanner {
5+
public:
6+
StockSpanner() {
7+
8+
}
9+
10+
int next(int price) {
11+
int result = 1;
12+
while (!s_.empty() && s_.top().first <= price) {
13+
result += s_.top().second; s_.pop();
14+
}
15+
s_.emplace(price, result);
16+
return result;
17+
}
18+
19+
private:
20+
stack<pair<int, int>> s_;
21+
};
22+
23+
/**
24+
* Your StockSpanner object will be instantiated and called as such:
25+
* StockSpanner obj = new StockSpanner();
26+
* int param_1 = obj.next(price);
27+
*/
28+

0 commit comments

Comments
 (0)