


Input: ‘n’ = 7, ‘prices’ = [100, 80, 60, 70, 60, 75, 85]
Output: [1, 1, 1, 2, 1, 4, 6]
Explanation:
On the sixth day, when the stock price was 75,
The span came out to be 4 because the last three prices(plus today) were less than the current or the sixth day's price.
Similarly, we can deduce the remaining results.
You don’t need to print anything. Just implement the given function
The first line of each test case contains an integer ‘n’ denoting the number of days.
The second line of each test case contains ‘n’ integers denoting the prices of the stocks.
Return an array that contains the stock’s span for each day.
In the naive approach, We can iterate over the prices array. For the ‘ith’ day, we can start iterating over the previous prices starting from (i-1)th day, and then for each last day, if the price is less than the price of the ith day, then we increase the span of the ith day by 1 otherwise we stop iterating over the previous prices.
We can use a monotonic stack to find the closest previous day with a price greater than or equal to the current day's price. Then we iterate over the ‘prices’ array, and for the ith day total span is calculated as i - the previous day (with a price greater than or equal to that of the ith day). Because all the days between them will be consecutive and their prices will be smaller than that of ith day.