Skip to content

Commit dc24ae9

Browse files
Time: N/A (0%), Space: N/A (0%) - LeetHub
1 parent 2ca195e commit dc24ae9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class StockSpanner {
2+
stack<pair<int,int>> s; //1st is index and 2nd is value
3+
int index;
4+
public:
5+
StockSpanner() {
6+
ios::sync_with_stdio(false);
7+
cin.tie(0);
8+
index = -1;
9+
}
10+
11+
int next(int price) {
12+
index +=1;
13+
14+
while(!s.empty() && s.top().second<=price) //Find the previous greater element
15+
s.pop();
16+
//If there is no previous greater element
17+
if(s.empty())
18+
{ s.push({index,price}); return index+1; }
19+
20+
int result = s.top().first;
21+
s.push({index,price});
22+
return index-result;
23+
}
24+
};

0 commit comments

Comments
 (0)