Advertisement
Iam_Sandeep

901. Online Stock Span

Jul 23rd, 2022
1,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. '''
  2. Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.
  3.  
  4. The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today's price.
  5.  
  6. For example, if the price of a stock over the next 7 days were [100,80,60,70,60,75,85], then the stock spans would be [1,1,1,2,1,4,6].
  7. Implement the StockSpanner class:
  8.  
  9. StockSpanner() Initializes the object of the class.
  10. int next(int price) Returns the span of the stock's price given that today's price is price.
  11.  
  12. '''
  13. class StockSpanner:
  14.  
  15.     def __init__(self):
  16.         self.st=[]
  17.        
  18.  
  19.     def next(self, p: int) -> int:
  20.         st=self.st#(price,span)
  21.         res=1
  22.         while st and st[-1][0]<=p:
  23.             res=res+st[-1][1]
  24.             st.pop()
  25.         st.append((p,res))
  26.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement