ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 901. Online Stock Span

2022-06-19 12:05:23  阅读:151  来源: 互联网

标签:901 Span price next StockSpanner stockSpanner return Stock stock


原题链接在这里: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  */

类似Daily Temperatures.

标签:901,Span,price,next,StockSpanner,stockSpanner,return,Stock,stock
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16390205.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有