All problems
Stock Span
Problem
Compute the stock span for each day.
The span of day i is the number of consecutive days ending at day i whose price is less than or equal to prices[i]. Maintain a stack of indices with strictly decreasing prices, popping smaller or equal prices before answering.
Return the list of spans.
Hint
The span is the distance to the nearest previous day with a strictly higher price.
Test cases
case 1
in: [[100,80,60,70,60,75,85]]
expected: [1,1,1,2,1,4,6]
case 2
in: [[10,20,30]]
expected: [1,2,3]
case 3
in: [[30,20,10]]
expected: [1,1,1]
case 4
in: [[]]
expected: []
Starter codePython
def stock_span(prices):
# Your code here
pass4 test cases · run your code against them in the editor