Problem · Array

Stock Span I

Learn this problem
MediumFreshworks logoFreshworksFULLTIMEPHONE SCREEN

Problem 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 <= 200000
  • 1 <= prices[i] <= 2^31 - 1

More Freshworks problems

drafts saved locally
public int[] stockSpan(int[] prices) {
  // write your code here
}
prices[100,80,60,70,60,75,85]
expected[1,1,1,2,1,4,6]
checking account