Difficult:Medium
題目
Design an algorithm that collects daily price quotes for some stock and returns the span of that stock’s price for the current day.
The span of the stock’s price today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today’s price.
For example, if the price of a stock over the next 7 days were [100,80,60,70,60,75,85], then the stock spans would be [1,1,1,2,1,4,6].
Implement the StockSpanner class:
StockSpanner() Initializes the object of the class.
int next(int price) Returns the span of the stock’s price given that today’s price is price.
翻譯
設計一種算法,收集某隻股票的每日報價並返回該股票當天價格的跨度。
今天股票價格的跨度定義為股票價格小於或等於今天價格的最大連續天數(從今天開始向後)。
例如,如果未來 7 天的股票價格為 [100,80,60,70,60,75,85],則股票跨度將為 [1,1,1,2,1,4, 6]。
實現 StockSpanner 類:
StockSpanner() 初始化類的對象。
int next(int price) 假設今天的價格是價格,則返回股票價格的跨度。
範例
Example 1:
1 | Input |
解題思路
- 用一個陣列儲存
- next中num用來記數,與陣列的數做比較,若價錢小於數時跳出迴圈返回記數
程式碼
1 | var StockSpanner = function() { |