616. Online Stock Span

MediumStackMonotonic StackStackDesign

Process a stream of daily stock prices. For each new price, return its span — the number of consecutive days (ending today) whose price was less than or equal to today's. Return the array of spans in order. The input is JSON {prices}.

Input: JSON {prices}.

Output: Array — the span after each price.

Examples

Example 1
Input: {"prices":[100,80,60,70,60,75,85]}
Output: [1,1,1,2,1,4,6]
Explanation: Streaming stock spans.
Example 2
Input: {"prices":[31,41,48,59,79]}
Output: [1,2,3,4,5]
Explanation: Monotonic rise.
Example 3
Input: {"prices":[5]}
Output: [1]
Explanation: First day.

Constraints

Asked by

BloombergMicrosoftAmazonIBMAdobeGoogle
Solve this problem in the editor →