LeetCode 901. Online Stock Span
原题链接在这里:https://leetcode.com/problems/online-stock-span/
题目:
Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.
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.
- 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]
.
Implement the StockSpanner
class:
StockSpanner()
Initializes the object of the class.int next(int price)
Returns the span of the stock's price given that today's price isprice
.
Example 1:
Input ["StockSpanner", "next", "next", "next", "next", "next", "next", "next"] [[], [100], [80], [60], [70], [60], [75], [85]] Output [null, 1, 1, 1, 2, 1, 4, 6] Explanation StockSpanner stockSpanner = new StockSpanner(); stockSpanner.next(100); // return 1 stockSpanner.next(80); // return 1 stockSpanner.next(60); // return 1 stockSpanner.next(70); // return 2 stockSpanner.next(60); // return 1 stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price. stockSpanner.next(85); // return 6
Constraints:
1 <= price <= 105
- At most
104
calls will be made tonext
.
题解:
Have a stack maintain pair of stock price and corresponding span.
When a new price comes in, pop the stack if top of stack price <= current price. Accumlate the span into current span.
And finally return current span and push into stack.
Time Complexity: O(1). Each price is in and out of stack, overral it takes 2*n time for n price. The average is O(1).
Space: O(n).
AC Java:
1 class StockSpanner { 2 Stack<int []> stk; 3 public StockSpanner() { 4 stk = new Stack<>(); 5 } 6 7 public int next(int price) { 8 int res = 1; 9 while(!stk.isEmpty() && stk.peek()[0] <= price){ 10 int [] cur = stk.pop(); 11 res += cur[1]; 12 } 13 14 stk.push(new int[]{price, res}); 15 return res; 16 } 17 } 18 19 /** 20 * Your StockSpanner object will be instantiated and called as such: 21 * StockSpanner obj = new StockSpanner(); 22 * int param_1 = obj.next(price); 23 */
AC C++:
1 class StockSpanner { 2 public: 3 StockSpanner() { 4 5 } 6 7 int next(int price) { 8 int res = 1; 9 while(!stk.empty() && stk.top().first <= price){ 10 res += stk.top().second; 11 stk.pop(); 12 } 13 14 stk.push({price, res}); 15 return res; 16 } 17 private: 18 stack<pair<int, int>> stk; 19 }; 20 21 /** 22 * Your StockSpanner object will be instantiated and called as such: 23 * StockSpanner* obj = new StockSpanner(); 24 * int param_1 = obj->next(price); 25 */
AC Python:
1 class StockSpanner: 2 3 def __init__(self): 4 self.stk = [] 5 6 def next(self, price: int) -> int: 7 res = 1 8 while(self.stk and self.stk[-1][0] <= price): 9 res += self.stk.pop()[1] 10 self.stk.append([price, res]) 11 return res 12 13 # Your StockSpanner object will be instantiated and called as such: 14 # obj = StockSpanner() 15 # param_1 = obj.next(price)