593. Stock Span Problem

EasyStackMonotonic StackStack

Given daily stock prices, the span for a day is the number of consecutive days up to and including that day for which the price was less than or equal to that day's price. Return the array of spans. The input is JSON {prices}.

Input: JSON {prices}.

Output: Array — the span for each day.

Examples

Example 1
Input: {"prices":[100,80,60,70,60,75,85]}
Output: [1,1,1,2,1,4,6]
Explanation: Consecutive days not exceeding each price.
Example 2
Input: {"prices":[5]}
Output: [1]
Explanation: A single day has span 1.
Example 3
Input: {"prices":[10,4,5,90,120,80]}
Output: [1,1,2,4,5,1]
Explanation: Spans grow on rising runs.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →