901:股票价格跨度
This commit is contained in:
parent
478dc4ce12
commit
25db7ef69c
@ -0,0 +1,82 @@
|
||||
//编写一个 StockSpanner 类,它收集某些股票的每日报价,并返回该股票当日价格的跨度。
|
||||
//
|
||||
// 今天股票价格的跨度被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包括今天)。
|
||||
//
|
||||
// 例如,如果未来7天股票的价格是 [100, 80, 60, 70, 60, 75, 85],那么股票跨度将是 [1, 1, 1, 2, 1, 4, 6]。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
// 输入:["StockSpanner","next","next","next","next","next","next","next"], [[],[10
|
||||
//0],[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) 小于或等于今天的价格。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 调用 StockSpanner.next(int price) 时,将有 1 <= price <= 10^5。
|
||||
// 每个测试用例最多可以调用 10000 次 StockSpanner.next。
|
||||
// 在所有测试用例中,最多调用 150000 次 StockSpanner.next。
|
||||
// 此问题的总时间限制减少了 50%。
|
||||
//
|
||||
// Related Topics 栈
|
||||
// 👍 116 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
//901:股票价格跨度
|
||||
public class OnlineStockSpan {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
// Solution solution = new OnlineStockSpan().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class StockSpanner {
|
||||
Stack<Integer> prices, nums;
|
||||
|
||||
public StockSpanner() {
|
||||
prices = new Stack<>();
|
||||
nums = new Stack<>();
|
||||
}
|
||||
|
||||
public int next(int price) {
|
||||
int num = 1;
|
||||
while (!prices.isEmpty() && prices.peek() <= price) {
|
||||
prices.pop();
|
||||
num += nums.pop();
|
||||
}
|
||||
prices.push(price);
|
||||
nums.push(num);
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your StockSpanner object will be instantiated and called as such:
|
||||
* StockSpanner obj = new StockSpanner();
|
||||
* int param_1 = obj.next(price);
|
||||
*/
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
37
LeetCode/src/main/java/leetcode/editor/cn/OnlineStockSpan.md
Normal file
37
LeetCode/src/main/java/leetcode/editor/cn/OnlineStockSpan.md
Normal file
@ -0,0 +1,37 @@
|
||||
<p>编写一个 <code>StockSpanner</code> 类,它收集某些股票的每日报价,并返回该股票当日价格的跨度。</p>
|
||||
|
||||
<p>今天股票价格的跨度被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包括今天)。</p>
|
||||
|
||||
<p>例如,如果未来7天股票的价格是 <code>[100, 80, 60, 70, 60, 75, 85]</code>,那么股票跨度将是 <code>[1, 1, 1, 2, 1, 4, 6]</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]]
|
||||
<strong>输出:</strong>[null,1,1,1,2,1,4,6]
|
||||
<strong>解释:</strong>
|
||||
首先,初始化 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) 小于或等于今天的价格。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>调用 <code>StockSpanner.next(int price)</code> 时,将有 <code>1 <= price <= 10^5</code>。</li>
|
||||
<li>每个测试用例最多可以调用 <code>10000</code> 次 <code>StockSpanner.next</code>。</li>
|
||||
<li>在所有测试用例中,最多调用 <code>150000</code> 次 <code>StockSpanner.next</code>。</li>
|
||||
<li>此问题的总时间限制减少了 50%。</li>
|
||||
</ol>
|
||||
<div><div>Related Topics</div><div><li>栈</li></div></div>\n<div><li>👍 116</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user