|
| 1 | +## 901. Online Stock Span |
| 2 | + |
| 3 | +### Question |
| 4 | +Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day. |
| 5 | + |
| 6 | +The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price. |
| 7 | + |
| 8 | +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]. |
| 9 | + |
| 10 | + ``` |
| 11 | +Example 1: |
| 12 | +
|
| 13 | +Input: ["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]] |
| 14 | +Output: [null,1,1,1,2,1,4,6] |
| 15 | +Explanation: |
| 16 | +First, S = StockSpanner() is initialized. Then: |
| 17 | +S.next(100) is called and returns 1, |
| 18 | +S.next(80) is called and returns 1, |
| 19 | +S.next(60) is called and returns 1, |
| 20 | +S.next(70) is called and returns 2, |
| 21 | +S.next(60) is called and returns 1, |
| 22 | +S.next(75) is called and returns 4, |
| 23 | +S.next(85) is called and returns 6. |
| 24 | +
|
| 25 | +Note that (for example) S.next(75) returned 4, because the last 4 prices |
| 26 | +(including today's price of 75) were less than or equal to today's price. |
| 27 | +``` |
| 28 | + |
| 29 | +Note: |
| 30 | +* Calls to StockSpanner.next(int price) will have 1 <= price <= 10^5. |
| 31 | +* There will be at most 10000 calls to StockSpanner.next per test case. |
| 32 | +* There will be at most 150000 calls to StockSpanner.next across all test cases. |
| 33 | +* The total time limit for this problem has been reduced by 75% for C++, and 50% for all other languages. |
| 34 | + |
| 35 | +### Solution: |
| 36 | +* Method 1: Monotic stack: |
| 37 | + * Keep the stack mono decrease |
| 38 | + * Step 1 100 [100, 1] stack is empty and nothing left is small than 100, push [100, 1] to stack. |
| 39 | + * Step 2 80 [100, 1][80, 1] 100 > 80, we push[80, 1] to the stack. |
| 40 | + * Step 3 60 [100, 1][80, 1][60, 1] same as previous step, we push [60, 1] to the stack. |
| 41 | + * Step 4 70 [100, 1][80, 1][70, 2] 60 < 70, so we pop it from the stack and add its count to current one |
| 42 | + * Step 5 60 [100, 1][80, 1][70, 2][60, 1] still decreasing, push to stack. |
| 43 | + * Step 6 75 [100, 1][80, 1][75, 4] 75 > 60 and 75 > 70, pop that two elements and add their counts to current one. |
| 44 | + * Step 7 85 [100, 1][85, 6] same as previous step, pop 80 and 75. |
| 45 | + ```Java |
| 46 | + class StockSpanner { |
| 47 | + private Stack<int[]> stack; |
| 48 | + public StockSpanner() { |
| 49 | + this.stack = new Stack<>(); |
| 50 | + } |
| 51 | + |
| 52 | + public int next(int price) { |
| 53 | + int count = 1; |
| 54 | + while(!stack.isEmpty() && stack.peek()[0] <= price){ |
| 55 | + count += stack.pop()[1]; |
| 56 | + } |
| 57 | + stack.push(new int[]{price, count}); |
| 58 | + return count; |
| 59 | + } |
| 60 | + } |
| 61 | + /** |
| 62 | + * Your StockSpanner object will be instantiated and called as such: |
| 63 | + * StockSpanner obj = new StockSpanner(); |
| 64 | + * int param_1 = obj.next(price); |
| 65 | + */ |
| 66 | + ``` |
0 commit comments