The stock span problem

i could not understand the implementation of code used in the video. iwant to understand the logic by starting using stack for slving this problem. please help!!!

1 Like

The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}
For computing this problem we will use stack data structure. Time complexity of this method is O(N) where N is the size of stock price array.
In this method S[i] (stock span value) on day i can be easily computed if we know the closest day preceding i, such that the price is greater than on that day than the price on day i.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like


Ma’am please help i am unable to find the problem…

Hello @Shorya-Sharma-1269737519832469,

You may post a separate doubt for your problem.
The fellow TAs will help you.

BTW, you can tag Aastha there if you specifically Want her to reply.