
理解题目,对于第 i 天,要求的是前 i - 1 天所满足条件的跨度
思路:
代码单调栈实现:
class StockSpanner:
def __init__(self):
self.l = [[1000000, 0]]
def next(self, price: int) -> int:
if price < self.l[-1][0]:
self.l.append([price, 1])
return 1
ans = 1
while price >= self.l[-1][0]:
ans += self.l[-1][1]
self.l.pop()
self.l.append([price, ans])
return ans