← Home

901. Online Stock Span

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
data structure designC++Markdown
901

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 901. Online Stock Span, the solution in this repository is mainly a data structure design solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: data structure design, stack.

The notes already sitting in the source point us in the right direction:

  • decreasing stack
  • time: O(query), space: O(query)

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are next.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(query), space: O(query)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//decreasing stack
02//Runtime: 612 ms, faster than 6.81% of C++ online submissions for Online Stock Span.
03//Memory Usage: 84.6 MB, less than 7.69% of C++ online submissions for Online Stock Span.
04//time: O(query), space: O(query)
05class StockSpanner {
06public:
07    //decreasing stack(the bottom element is the largest)
08    stack<int> prices;
09    //number of elements popped before pushing corresponding "price" + 1
10    stack<int> weights;
11    
12    StockSpanner() {
13        
14    }
15    
16    int next(int price) {
17        int w = 1; //current element
18        while(!prices.empty() && prices.top() <= price){
19            prices.pop();
20            w += weights.top(); weights.pop(); //number of elements skipped by prices.top()
21        }
22        
23        prices.push(price);
24        weights.push(w);
25        return w;
26    }
27};
28
29/**
30 * Your StockSpanner object will be instantiated and called as such:
31 * StockSpanner* obj = new StockSpanner();
32 * int param_1 = obj->next(price);
33 */

Cost

Complexity

Time
O(query), space: O(query)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.