2022-10-21 栈底垫上杯底,,初始化技巧

class StockSpanner:
    def __init__(self):
        #最厉害的这里,初始化技巧
        self.stack = [(-1, inf)]   
        self.idx = -1

    def next(self, price: int) -> int:
        self.idx += 1
        while price >= self.stack[-1][1]:
            self.stack.pop()
        self.stack.append((self.idx, price))
        return self.idx - self.stack[-2][0]

#作者:LeetCode-Solution  官方有优美的栈垫底,,



'''class StockSpanner:

    def __init__(self):
        self.la=[]
        self.index=0

    def next(self, price: int) -> int:
        cha=1
        self.index+=1 
        while self.la !=[] and self.la[-1][0] <= price:
            self.la.pop()
        
        if not self.la:
            self.la.append((price,self.index))
            return self.index

        a=self.la[-1][1]
        self.la.append((price,self.index))
        cha=self.index -a 
        return cha'''


你可能感兴趣的:(2022-10-21 栈底垫上杯底,,初始化技巧)