Price Streak Span
For each day i, its *span* is the number of consecutive days ending at day i (including it) whose price is less than or equal to prices[i].
Return the span of every day.
Example 1
Input: prices = [100, 80, 60, 70, 60, 75, 85] Output: [1, 1, 1, 2, 1, 4, 6]
Example 2
Input: prices = [5, 5, 5] Output: [1, 2, 3]
Constraints
- 0 <= prices.length <= 10^5
- 1 <= prices[i] <= 10^9
Hint 1
The span ends at the nearest earlier day with a strictly higher price.
Hint 2
A stack of indices with decreasing prices finds that day in amortised O(1).
Discussion 0
Join free to post in the discussion.
Good practice for: Data Scientist, Data Engineer, Machine Learning Engineer, AI Engineer (LLM & Generative AI), Backend Developer, Blockchain Developer
Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.