classStockSpanner:def__init__(self):self.stk=[]defnext(self,price:int)->int:cnt=1whileself.stkandself.stk[-1][0]<=price:cnt+=self.stk.pop()[1]self.stk.append((price,cnt))returncnt# Your StockSpanner object will be instantiated and called as such:# obj = StockSpanner()# param_1 = obj.next(price)
1 2 3 4 5 6 7 8 9101112131415161718192021
classStockSpanner{privateDeque<int[]>stk=newArrayDeque<>();publicStockSpanner(){}publicintnext(intprice){intcnt=1;while(!stk.isEmpty()&&stk.peek()[0]<=price){cnt+=stk.pop()[1];}stk.push(newint[]{price,cnt});returncnt;}}/** * Your StockSpanner object will be instantiated and called as such: * StockSpanner obj = new StockSpanner(); * int param_1 = obj.next(price); */
1 2 3 4 5 6 7 8 9101112131415161718192021222324
classStockSpanner{public:StockSpanner(){}intnext(intprice){intcnt=1;while(!stk.empty()&&stk.top().first<=price){cnt+=stk.top().second;stk.pop();}stk.emplace(price,cnt);returncnt;}private:stack<pair<int,int>>stk;};/** * Your StockSpanner object will be instantiated and called as such: * StockSpanner* obj = new StockSpanner(); * int param_1 = obj->next(price); */
1 2 3 4 5 6 7 8 910111213141516171819202122232425
typeStockSpannerstruct{stk[]pair}funcConstructor()StockSpanner{returnStockSpanner{[]pair{}}}func(this*StockSpanner)Next(priceint)int{cnt:=1forlen(this.stk)>0&&this.stk[len(this.stk)-1].price<=price{cnt+=this.stk[len(this.stk)-1].cntthis.stk=this.stk[:len(this.stk)-1]}this.stk=append(this.stk,pair{price,cnt})returncnt}typepairstruct{price,cntint}/** * Your StockSpanner object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Next(price); */
1 2 3 4 5 6 7 8 910111213141516171819202122
classStockSpanner{privatestk:number[][];constructor(){this.stk=[];}next(price:number):number{letcnt=1;while(this.stk.length&&this.stk.at(-1)[0]<=price){cnt+=this.stk.pop()[1];}this.stk.push([price,cnt]);returncnt;}}/** * Your StockSpanner object will be instantiated and called as such: * var obj = new StockSpanner() * var param_1 = obj.next(price) */
1 2 3 4 5 6 7 8 910111213141516171819202122232425
usestd::collections::VecDeque;structStockSpanner{stk:VecDeque<(i32,i32)>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implStockSpanner{fnnew()->Self{Self{stk:vec![(i32::MAX,-1)].into_iter().collect(),}}fnnext(&mutself,price:i32)->i32{letmutcnt=1;whileself.stk.back().unwrap().0<=price{cnt+=self.stk.pop_back().unwrap().1;}self.stk.push_back((price,cnt));cnt}}