Problem · Array

Online Stock Span

Learn this problem
MediumTokopedia logoTokopediaFULLTIMEONSITE INTERVIEW

Problem statement

You receive daily stock prices in chronological order. For each day, its span is the maximum number of consecutive days ending on that day whose prices are less than or equal to the current price.

Return the span for every price in prices. The array is a finite runner wrapper for consecutive calls to the canonical online next operation: process left to right and never look ahead.

Function

calculateStockSpans(prices: int[]) → int[]

Examples

Example 1

prices = [100,80,60,70,60,75,85]return = [1,1,1,2,1,4,6]

At price 75, the consecutive suffix [60,70,60,75] contributes span four; price 100 blocks the final price 85 at span six.

Constraints

  • 1 <= prices.length <= 10000.
  • 1 <= prices[i] <= 100000.

More Tokopedia problems

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