Welcome to Subscribe On Youtube
901. Online Stock Span
Description
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 is4
because starting from today, the price of the stock was less than or equal2
for4
consecutive 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 is3
because starting from today, the price of the stock was less than or equal8
for3
consecutive 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^{4}
calls will be made tonext
.
Solutions
Solution 1: Monotonic Stack
Based on the problem description, we know that for the current day’s price $price$, we start from this price and look backwards to find the first price that is larger than this price. The difference in indices $cnt$ between these two prices is the span of the current day’s price.
This is actually a classic monotonic stack model, where we find the first element larger than the current element on the left.
We maintain a stack where the prices from the bottom to the top of the stack are monotonically decreasing. Each element in the stack is a $(price, cnt)$ data pair, where $price$ represents the price, and $cnt$ represents the span of the current price.
When the price $price$ appears, we compare it with the top element of the stack. If the price of the top element of the stack is less than or equal to $price$, we add the span $cnt$ of the current day’s price to the span of the top element of the stack, and then pop the top element of the stack. This continues until the price of the top element of the stack is greater than $price$, or the stack is empty.
Finally, we push $(price, cnt)$ onto the stack and return $cnt$.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of times next(price)
is called.

class StockSpanner { private Deque<int[]> stk = new ArrayDeque<>(); public StockSpanner() { } public int next(int price) { int cnt = 1; while (!stk.isEmpty() && stk.peek()[0] <= price) { cnt += stk.pop()[1]; } stk.push(new int[] {price, cnt}); return cnt; } } /** * Your StockSpanner object will be instantiated and called as such: * StockSpanner obj = new StockSpanner(); * int param_1 = obj.next(price); */

class StockSpanner { public: StockSpanner() { } int next(int price) { int cnt = 1; while (!stk.empty() && stk.top().first <= price) { cnt += stk.top().second; stk.pop(); } stk.emplace(price, cnt); return cnt; } private: stack<pair<int, int>> stk; }; /** * Your StockSpanner object will be instantiated and called as such: * StockSpanner* obj = new StockSpanner(); * int param_1 = obj>next(price); */

class StockSpanner: def __init__(self): self.stk = [] def next(self, price: int) > int: cnt = 1 while self.stk and self.stk[1][0] <= price: cnt += self.stk.pop()[1] self.stk.append((price, cnt)) return cnt # Your StockSpanner object will be instantiated and called as such: # obj = StockSpanner() # param_1 = obj.next(price)

type StockSpanner struct { stk []pair } func Constructor() StockSpanner { return StockSpanner{[]pair{} } } func (this *StockSpanner) Next(price int) int { cnt := 1 for len(this.stk) > 0 && this.stk[len(this.stk)1].price <= price { cnt += this.stk[len(this.stk)1].cnt this.stk = this.stk[:len(this.stk)1] } this.stk = append(this.stk, pair{price, cnt}) return cnt } type pair struct{ price, cnt int } /** * Your StockSpanner object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Next(price); */

class StockSpanner { private stk: number[][]; constructor() { this.stk = []; } next(price: number): number { let cnt = 1; while (this.stk.length && this.stk.at(1)[0] <= price) { cnt += this.stk.pop()[1]; } this.stk.push([price, cnt]); return cnt; } } /** * Your StockSpanner object will be instantiated and called as such: * var obj = new StockSpanner() * var param_1 = obj.next(price) */

use std::collections::VecDeque; struct StockSpanner { stk: VecDeque<(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 { Self { stk: vec![(i32::MAX, 1)].into_iter().collect(), } } fn next(&mut self, price: i32) > i32 { let mut cnt = 1; while self.stk.back().unwrap().0 <= price { cnt += self.stk.pop_back().unwrap().1; } self.stk.push_back((price, cnt)); cnt } }/** * Your StockSpanner object will be instantiated and called as such: * let obj = StockSpanner::new(); * let ret_1: i32 = obj.next(price); */