We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2ca195e commit dc24ae9Copy full SHA for dc24ae9
0937-online-stock-span/0937-online-stock-span.java
@@ -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