901. Online Stock Span
Description of Problem
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 in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to the price of that day.
- For example, if the prices of the stock in the last four days is
[7,2,1,2]and the price of the stock today is2, then the span of today is4because starting from today, the price of the stock was less than or equal2for4consecutive days. - Also, if the prices of the stock in the last four days is
[7,34,1,2]and the price of the stock today is8, then the span of today is3because starting from today, the price of the stock was less than or equal8for3consecutive days.
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 <= 10^5- At most
10^4calls will be made tonext.
Solution
Tags: Monotonic Stack
Explanation
Create a monotonic stack that stores tuples (price, days) (days means the number of consecutive days that price of stock is smaller than or equal to price) in decreasing order in terms of price along stack grwoing direction. That means the top of the stack is always the smallest element.
When a new element price comes, there are two possible cases:
- If the top element of the stack is greater, that means there is no consecutive preivous days where stock price is smaller than or equal to the new price. Hence, insert
(price, 1)into the stack. - Otherwise, keep remove the top element and increment
countbydays. After all, insert(price, count+1).
For example:
1. [(100,1)] // Just insert 100
2. [(100,1), (80,1)] // Just insert 80
3. [(100,1), (80,1), (60,1)] // Just insert 60
4. [(100,1), (80,1), (70,2)] // In order to insert 70, pop 60
5. [(100,1), (80,1), (70,2), (60, 1)] // Just insert 60
6. [(100,1), (80,1), (75, 4)] // In order to insert 70, pop 60 and 70
7. [(100,1), (85, 6)] // In order to insert 70, pop 65 and 80
Code
struct StockSpanner {
monotonic_stack : Vec<(i32,i32)>
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl StockSpanner {
fn new() -> Self {
StockSpanner {
monotonic_stack : Vec::new()
}
}
fn next(&mut self, price: i32) -> i32 {
if let Some(&(top, _)) = self.monotonic_stack.last(){
if top > price {
self.monotonic_stack.push((price, 1));
return 1;
}else {
let mut pop_count = 0;
// keep popping smaller elements
while let Some(&(top, days)) = self.monotonic_stack.last(){
if top <= price {
self.monotonic_stack.pop();
pop_count+=days;
}else{
break;
}
}
self.monotonic_stack.push((price, pop_count+1));
return pop_count+1;
}
}else{
self.monotonic_stack.push((price, 1));
return 1;
}
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* let obj = StockSpanner::new();
* let ret_1: i32 = obj.next(price);
*/
Complexity
- n is the number of
int next(int price)requests
Time Complexity
- \(T(n) = O(n) \)
Auxiliary Space
- \(S(n) = O(n) \)