[Swift]LeetCode901. 股票價格跨度 | Online Stock Span

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-tuyhavtc-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day.git

The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price.github

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]微信

Example 1:app

Input: ["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]] Output: [null,1,1,1,2,1,4,6] Explanation: First, S = StockSpanner() is initialized. Then: S.next(100) is called and returns 1, S.next(80) is called and returns 1, S.next(60) is called and returns 1, S.next(70) is called and returns 2, S.next(60) is called and returns 1, S.next(75) is called and returns 4, S.next(85) is called and returns 6. Note that (for example) S.next(75) returned 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price. 

Note:less

  1. Calls to StockSpanner.next(int price) will have 1 <= price <= 10^5.
  2. There will be at most 10000 calls to StockSpanner.next per test case.
  3. There will be at most 150000 calls to StockSpanner.next across all test cases.
  4. The total time limit for this problem has been reduced by 75% for C++, and 50% for all other languages.

編寫一個 StockSpanner 類,它收集某些股票的每日報價,並返回該股票當日價格的跨度。測試

今天股票價格的跨度被定義爲股票價格小於或等於今天價格的最大連續日數(從今天開始往回數,包括今天)。this

例如,若是將來7天股票的價格是 [100, 80, 60, 70, 60, 75, 85],那麼股票跨度將是 [1, 1, 1, 2, 1, 4, 6]。 spa

示例:code

輸入:["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]]
輸出:[null,1,1,1,2,1,4,6]
解釋:
首先,初始化 S = StockSpanner(),而後:
S.next(100) 被調用並返回 1,
S.next(80) 被調用並返回 1,
S.next(60) 被調用並返回 1,
S.next(70) 被調用並返回 2,
S.next(60) 被調用並返回 1,
S.next(75) 被調用並返回 4,
S.next(85) 被調用並返回 6。

注意 (例如) S.next(75) 返回 4,由於截至今天的最後 4 個價格
(包括今天的價格 75) 小於或等於今天的價格。 

提示:

  1. 調用 StockSpanner.next(int price) 時,將有 1 <= price <= 10^5
  2. 每一個測試用例最多能夠調用  10000 次 StockSpanner.next
  3. 在全部測試用例中,最多調用 150000 次 StockSpanner.next
  4. 此問題的總時間限制減小了 50%。

Runtime: 840 ms
Memory Usage: 23 MB
 1 class StockSpanner {
 2     var spans:[Int]
 3     var prices:[Int]
 4     var index:Int
 5 
 6     init() {
 7         spans = [Int](repeating:0,count:10_000)
 8         prices = [Int](repeating:0,count:10000)
 9         index = -1
10     }
11     
12     func next(_ price: Int) -> Int {
13         index += 1
14         prices[index] = price
15         if index == 0 || price < prices[index - 1]
16         {
17             spans[index] = 1
18             return 1
19         }
20         var previousIndex:Int = index - 1
21         var span:Int = 1
22         while (previousIndex >= 0 && price >= prices[previousIndex])
23         {
24             span += spans[previousIndex]
25             previousIndex -= spans[previousIndex]
26         }
27         spans[index] = span
28         return span
29     }
30 }
31 
32 /**
33  * Your StockSpanner object will be instantiated and called as such:
34  * let obj = StockSpanner()
35  * let ret_1: Int = obj.next(price)
36  */

892ms

 1 class StockSpanner {
 2 
 3     private var s = [(Int, Int)]()
 4     init() {
 5         
 6     }
 7     
 8     func next(_ price: Int) -> Int {
 9         var sum = 1
10         while !s.isEmpty, s.last!.0 <= price {
11             sum += s.removeLast().1
12         }
13         s.append((price, sum))
14         return sum
15     }
16 }

928ms

 1 class StockSpanner {
 2 
 3     init() {
 4         
 5     }
 6     
 7     struct PriceSpan {
 8         let price: Int
 9         let span: Int
10     }
11     
12     var stack = [PriceSpan]()
13     
14     func next(_ price: Int) -> Int {
15         guard stack.count > 0 else {
16             stack.append(PriceSpan(price: price, span: 1))
17             return 1
18         }
19         
20         var span = 1
21         while stack.last != nil && stack.last!.price <= price {
22             span += stack.last!.span
23             stack.removeLast()
24         }
25         
26         stack.append(PriceSpan(price: price, span: span))
27         return span
28     }
29 }
30 
31 /**
32  * Your StockSpanner object will be instantiated and called as such:
33  * let obj = StockSpanner()
34  * let ret_1: Int = obj.next(price)
35  */

1036ms

 1 class StockSpanner {
 2     private var span: [Int] = []
 3     private var stack: [StockSpan] = []
 4     init() {
 5         
 6     }
 7     
 8     func next(_ price: Int) -> Int {
 9         var stockSpan = StockSpan(price:price, span: 1)
10         while !stack.isEmpty && stack.last!.price <= stockSpan.price {
11             let removed = stack.removeLast()
12             stockSpan.span += removed.span
13         }
14         stack.append(stockSpan)
15         return stockSpan.span
16     }
17     
18     struct StockSpan {
19         let price: Int 
20         var span: Int
21     }
22 }
23 
24 /**
25  * Your StockSpanner object will be instantiated and called as such:
26  * let obj = StockSpanner()
27  * let ret_1: Int = obj.next(price)
28  */ 

 1 class StockSpanner {
 2     var prices: [Int] = []
 3     var days: [Int] = []
 4     init() {
 5         
 6     }
 7     
 8     func next(_ price: Int) -> Int {
 9         if prices.isEmpty || prices.last! > price {
10             prices.append(price)
11             days.append(1)
12             return 1
13         }
14         var index = prices.count - 1
15         var res = 1
16         while index >= 0 && prices[index] <= price {
17             res += days[index]
18             index -= days[index]
19         }
20         prices.append(price)
21         days.append(res)
22         return res
23     }
24 }
25 
26 /**
27  * Your StockSpanner object will be instantiated and called as such:
28  * let obj = StockSpanner()
29  * let ret_1: Int = obj.next(price)
30  */

 1 class StockSpanner {
 2     
 3     private var elements : [(price : Int, conquered : Int)] = []
 4     init() {
 5         
 6     }
 7     
 8     func next(_ price: Int) -> Int {
 9         var conquered : Int = 1
10         while !elements.isEmpty && elements.last!.price <= price {
11             let removed = elements.removeLast()
12             conquered += removed.conquered
13         }
14         
15         elements.append((price: price, conquered: conquered))
16         return elements.last!.conquered
17     }
18 }
19 
20 /**
21  * Your StockSpanner object will be instantiated and called as such:
22  * let obj = StockSpanner()
23  * let ret_1: Int = obj.next(price)
24  */
相關文章
相關標籤/搜索