Skip to content

Commit 595f8f6

Browse files
authoredApr 20, 2023
Create 0901-online-stock-span.cpp
1 parent b6dc3f2 commit 595f8f6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
 

Diff for: ‎cpp/0901-online-stock-span.cpp

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

0 commit comments

Comments
 (0)