Afzal has been working with an organization called 'Money Traders for the past few years. The organization is in the money trading business. His manager assigned him a task.
Given an array ’prices’ which denotes stock prices for ’n’ days, e.g., 'prices[ i ]' = price of the stock at ‘ith’ day, Find the stock's span for each day.
The span of the stock's price today is defined as the maximum number of consecutive days(starting from today and going backward) for which the price of the stock was less than today's price.
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.
Note:
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.
Output format:
Return an array that contains the stock’s span for each day.
4
2 1 2 4
1 1 2 4
Number of consecutive days with price smaller than 0th day(starting from 0th day) = 1
Number of consecutive days with price smaller than 1st day(starting from 1st day) = 1
Number of consecutive days with price smaller than 2nd day(starting from 2nd day) = 2
Number of consecutive days with price smaller than 3rd day(starting from 3rd day) = 4
6
20 12 1 28 16 20
1 1 1 4 1 2
Number of consecutive days with price smaller than 0th day(starting from 0th day) = 1
Number of consecutive days with price smaller than 1st day(starting from 1st day) = 1
Number of consecutive days with price smaller than 2nd day(starting from 2nd day) = 1
Number of consecutive days with price smaller than 3rd day(starting from 3rd day) = 4
Number of consecutive days with price smaller than 3rd day(starting from 4th day) = 1
Number of consecutive days with price smaller than 3rd day(starting from 5th day) = 2
The expected time complexity is O(n).
1 <= n <= 10^6
1 <= prices[i] <= 10^9
Time Limit: 1 sec
Can you think about a straightforward solution using two loops?
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.
The steps are as follows:-
Function findStockSpans(vector<int>& prices):
O(n * n ), Where ‘n’ denotes the number of days.
We are Iterating over the ‘prices’ array, and for each day, we are iterating over the previous days. Iterating over the previous days has the worst time complexity of O( n ), and iterating over the ‘prices’ array has an O ( n ) time complexity.
Hence the time complexity is O( n * n ).
O( 1 ).
We are not using any extra memory. The array required for ‘ANS’ does not count in Auxiliary Space.
Hence space complexity is O( 1 ).