Related: LeetCode 901 - Online Stock Span
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 span of 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 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}
Read full article from The Stock Span Problem | GeeksforGeeks
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 span of 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 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}
We see that S[i] 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. If such a day exists, let’s call it h(i), otherwise, we define h(i) = -1.
The span is now computed as S[i] = i – h(i). See the following diagram.
The span is now computed as S[i] = i – h(i). See the following diagram.
To implement this logic, we use a stack as an abstract data type to store the days i, h(i), h(h(i)) and so on. When we go from day i-1 to i, we pop the days when the price of the stock was less than or equal to price[i] and then push the value of day i back into the stack.
static
void
calculateSpan(
int
price[],
int
n,
int
S[])
{
// Create a stack and push index of first element to it
Stack<Integer> st=
new
Stack<>();
st.push(
0
);
// Span value of first element is always 1
S[
0
] =
1
;
// Calculate span values for rest of the elements
for
(
int
i =
1
; i < n; i++)
{
// Pop elements from stack while stack is not empty and top of
// stack is smaller than price[i]
while
(!st.empty() && price[st.peek()] <= price[i])
st.pop();
// If stack becomes empty, then price[i] is greater than all elements
// on left of it, i.e., price[0], price[1],..price[i-1]. Else price[i]
// is greater than elements after top of stack
S[i] = (st.empty())? (i +
1
) : (i - st.peek());
// Push this element to stack
st.push(i);
}
}
static
void
calculateSpan(
int
price[],
int
n,
int
S[])
{
// Span value of first day is always 1
S[
0
] =
1
;
// Calculate span value of remaining days by linearly checking
// previous days
for
(
int
i =
1
; i < n; i++)
{
S[i] =
1
;
// Initialize span value
// Traverse left while the next element on left is smaller
// than price[i]
for
(
int
j = i-
1
; (j>=
0
)&&(price[i]>=price[j]); j--)
S[i]++;
}
}