Complete the code to push the current index onto the stack.
stack.[1](i);The push method adds the current index i to the top of the stack.
Complete the condition to pop from the stack while the current price is higher than the price at the top index of the stack.
while (stack.length > 0 && prices[i] [1] prices[stack[stack.length - 1]]) { stack.pop(); }
We pop from the stack while the current price is greater than the price at the top of the stack.
Fix the error in calculating the span when the stack is empty.
spans[i] = stack.length === 0 ? [1] : i - stack[stack.length - 1];
If the stack is empty, the span is the entire length from day 0 to current day, which is i + 1.
Fill both blanks to complete the loop that calculates spans for all days.
for (let [1] = 0; [2] < prices.length; [2]++) { // loop body }
The loop variable is i, used consistently in initialization, condition, and increment.
Fill all three blanks to complete the function that returns the stock spans.
function calculateSpan(prices: number[]): number[] {
const spans: number[] = new Array(prices.length).fill(0);
const stack: number[] = [];
for (let [1] = 0; [2] < prices.length; [2]++) {
while (stack.length > 0 && prices[[3]] > prices[stack[stack.length - 1]]) {
stack.pop();
}
spans[[2]] = stack.length === 0 ? [2] + 1 : [2] - stack[stack.length - 1];
stack.push([2]);
}
return spans;
}j inside the while condition when loop uses i causes bugs.The loop variable i is used consistently for indexing in the loop, condition, and inside the while condition.
