@@ -86,13 +86,111 @@ Note that (for example) S.next(75) returned 4, because the last 4 prices
86
86
### ** Python3**
87
87
88
88
``` python
89
-
89
+ class StockSpanner :
90
+
91
+ def __init__ (self ):
92
+ self .stk = []
93
+
94
+ def next (self , price : int ) -> int :
95
+ res = 1
96
+ while self .stk and self .stk[- 1 ][0 ] <= price:
97
+ _, t = self .stk.pop()
98
+ res += t
99
+ self .stk.append([price, res])
100
+ return res
101
+
102
+ # Your StockSpanner object will be instantiated and called as such:
103
+ # obj = StockSpanner()
104
+ # param_1 = obj.next(price)
90
105
```
91
106
92
107
### ** Java**
93
108
94
109
``` java
110
+ class StockSpanner {
111
+ private Deque<int[]> stk;
112
+
113
+ public StockSpanner () {
114
+ stk = new ArrayDeque<> ();
115
+ }
116
+
117
+ public int next (int price ) {
118
+ int res = 1 ;
119
+ while (! stk. isEmpty() && stk. peek()[0 ] <= price) {
120
+ int [] t = stk. pop();
121
+ res += t[1 ];
122
+ }
123
+ stk. push(new int []{price, res});
124
+ return res;
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Your StockSpanner object will be instantiated and called as such:
130
+ * StockSpanner obj = new StockSpanner();
131
+ * int param_1 = obj.next(price);
132
+ */
133
+ ```
134
+
135
+ ### ** C++**
136
+
137
+ ``` cpp
138
+ class StockSpanner {
139
+ public:
140
+ stack<pair<int, int>> stk;
141
+
142
+ StockSpanner() {
143
+ }
144
+
145
+ int next (int price) {
146
+ int res = 1;
147
+ while (!stk.empty() && stk.top().first <= price)
148
+ {
149
+ pair<int, int> t = stk.top();
150
+ stk.pop();
151
+ res += t.second;
152
+ }
153
+ stk.push({price, res});
154
+ return res;
155
+ }
156
+ };
157
+
158
+ /**
159
+ * Your StockSpanner object will be instantiated and called as such:
160
+ * StockSpanner* obj = new StockSpanner();
161
+ * int param_1 = obj->next(price);
162
+ * /
163
+ ```
95
164
165
+ ### **Go**
166
+
167
+ ```go
168
+ type StockSpanner struct {
169
+ stk [][]int
170
+ }
171
+
172
+ func Constructor() StockSpanner {
173
+ return StockSpanner{
174
+ stk: make([][]int, 0),
175
+ }
176
+ }
177
+
178
+ func (this *StockSpanner) Next(price int) int {
179
+ res := 1
180
+ for len(this.stk) > 0 && this.stk[len(this.stk)-1][0] <= price {
181
+ t := this.stk[len(this.stk)-1]
182
+ res += t[1]
183
+ this.stk = this.stk[:len(this.stk)-1]
184
+ }
185
+ this.stk = append(this.stk, []int{price, res})
186
+ return res
187
+ }
188
+
189
+ /**
190
+ * Your StockSpanner object will be instantiated and called as such:
191
+ * obj := Constructor();
192
+ * param_1 := obj.Next(price);
193
+ */
96
194
```
97
195
98
196
### ** ...**
0 commit comments