LeetCode 901. Online Stock Span

原题链接在这里:https://leetcode.com/problems/online-stock-span/

题目:

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.

Example 1:

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
StockSpanner stockSpanner = new StockSpanner();
stockSpanner.next(100); // return 1
stockSpanner.next(80);  // return 1
stockSpanner.next(60);  // return 1
stockSpanner.next(70);  // return 2
stockSpanner.next(60);  // return 1
stockSpanner.next(75);  // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price.
stockSpanner.next(85);  // return 6

Constraints:

  • 1 <= price <= 105
  • At most 104 calls will be made to next.

题解:

Have a stack maintain pair of stock price and corresponding span.

When a new price comes in, pop the stack if top of stack price <= current price. Accumlate the span into current span.

And finally return current span and push into stack.

Time Complexity: O(1). Each price is in and out of stack, overral it takes 2*n time for n price. The average is O(1).

Space: O(n).

AC Java:

复制代码
 1 class StockSpanner {
 2     Stack<int []> stk;
 3     public StockSpanner() {
 4         stk = new Stack<>();
 5     }
 6     
 7     public int next(int price) {
 8         int res = 1;
 9         while(!stk.isEmpty() && stk.peek()[0] <= price){
10             int [] cur = stk.pop();
11             res += cur[1];
12         }
13         
14         stk.push(new int[]{price, res});
15         return res;
16     }
17 }
18 
19 /**
20  * Your StockSpanner object will be instantiated and called as such:
21  * StockSpanner obj = new StockSpanner();
22  * int param_1 = obj.next(price);
23  */
复制代码

AC C++:

复制代码
 1 class StockSpanner {
 2 public:
 3     StockSpanner() {
 4         
 5     }
 6     
 7     int next(int price) {
 8         int res = 1;
 9         while(!stk.empty() && stk.top().first <= price){
10             res += stk.top().second;
11             stk.pop();
12         }
13 
14         stk.push({price, res});
15         return res;
16     }
17 private:
18     stack<pair<int, int>> stk;
19 };
20 
21 /**
22  * Your StockSpanner object will be instantiated and called as such:
23  * StockSpanner* obj = new StockSpanner();
24  * int param_1 = obj->next(price);
25  */
复制代码

AC Python:

复制代码
 1 class StockSpanner:
 2 
 3     def __init__(self):
 4         self.stk = []
 5 
 6     def next(self, price: int) -> int:
 7         res = 1
 8         while(self.stk and self.stk[-1][0] <= price):
 9             res += self.stk.pop()[1]
10         self.stk.append([price, res])
11         return res
12 
13 # Your StockSpanner object will be instantiated and called as such:
14 # obj = StockSpanner()
15 # param_1 = obj.next(price)
复制代码

类似Next Greater Element IDaily Temperatures.

posted @   Dylan_Java_NYC  阅读(58)  评论(0)    收藏  举报
相关博文:
阅读排行:
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· .net clr 8年才修复的BUG,你让我损失太多了
· 一个神奇的JS代码,让浏览器在新的空白标签页运行我们 HTML 代码(createObjectURL
· 即时通信SSE和WebSocket对比
· 做Docx预览,一定要做这个神库!!
历史上的今天:
2019-06-19 LeetCode 652. Find Duplicate Subtrees
点击右上角即可分享
微信分享提示