Problem · Array
Stock Span I
Learn this problemProblem statement
Given an array prices of daily stock prices, return one span for every day in the same order.
The span for day i is the number of consecutive days ending at i for which each price is less than or equal to prices[i]. The current day is always included.
Function
stockSpan(prices: int[]) → int[]Examples
Example 1
prices = [100,80,60,70,60,75,85]return = [1,1,1,2,1,4,6]For price 75, the consecutive suffix [60, 70, 60, 75] has four prices less than or equal to 75, so its span is 4.
Example 2
prices = [10,10,10]return = [1,2,3]Equal previous prices extend the span because the comparison is less than or equal.
Constraints
1 <= prices.length <= 2000001 <= prices[i] <= 2^31 - 1