StockSpan


Submit solution

Points: 5
Time limit: 24.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
C, C++, Python

The stock span problem is a financial problem where we have a series of N daily price quotes for a stock and we need to calculate the span of the stock’s price for all N days. 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 its price on the given day.

Example:

Input1: N = 7

price1[] = [100 80 60 70 60 75 85]

Output1: 1 1 1 2 1 4 6

Explanation:

Traversing the given input span for 100 will be 1, 80 is smaller than 100 so the span is 1, 60 is smaller than 80 so the span is 1, 70 is greater than 60 so the span is 2 and so on. Hence the output will be 1 1 1 2 1 4 6.

Input2: N = 6

price2[] = [10 4 5 90 120 80]

Output2: 1 1 2 4 5 1

Explanation:

Traversing the given input span for 10 will be 1, 4 is smaller than 10 so the span will be 1, 5 is greater than 4 so the span will be 2 and so on. Hence, the output will be 1 1 2 4 5 1

Task:

Using stack you need to solve this stock span problem. Think creatively how a stack can help. What should be the time and space complexity of your implementation and why ?.

Test Case:

Input1

6

10 4 5 90 120 80

Output1

1 1 2 4 5 1

Input2

7

100 80 60 70 60 75 85

Output2

1 1 1 2 1 4 6


Comments

There are no comments at the moment.