← 返回 bloomberg 的题目列表Stock Price Tracker (Online Stock Span)
类型:online_judge
Stock Price Tracker
Implement an online stock price tracker. Stock prices arrive one day at a time in chronological order. For each incoming price price, immediately output its stock span.
The span of a day is the number of consecutive days ending today for which each day's price is less than or equal to today's price.
For example, for prices [100, 80, 60, 70, 60, 75, 85], the spans are [1, 1, 1, 2, 1, 4, 6].
Input Format
First line: an integer n, the number of consecutive days.
Second line: n integers prices[0..n-1], listed from earliest to latest.
Output Format
Print n integers separated by spaces, where the i-th integer is the stock span for day i.
Example
Input:
7
100 80 60 70 60 75 85
Output:
1 1 1 2 1 4 6
Constraints
1 <= n <= 2 * 10^5
1 <= prices[i] <= 10^9
The tracker must efficiently support many consecutive query / next(price) calls, with better total complexity than scanning backward linearly for every price.
Example
Input
7
100 80 60 70 60 75 85
Output
1 1 1 2 1 4 6