901. Online Stock Span Medium
1/**
2 * [901] Online Stock Span
3 *
4 * Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.
5 * 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.
6 *
7 * 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].
8 *
9 * Implement the StockSpanner class:
10 *
11 * StockSpanner() Initializes the object of the class.
12 * int next(int price) Returns the span of the stock's price given that today's price is price.
13 *
14 *
15 * Example 1:
16 *
17 * Input
18 * ["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
19 * [[], [100], [80], [60], [70], [60], [75], [85]]
20 * Output
21 * [null, 1, 1, 1, 2, 1, 4, 6]
22 * Explanation
23 * StockSpanner stockSpanner = new StockSpanner();
24 * stockSpanner.next(100); // return 1
25 * stockSpanner.next(80); // return 1
26 * stockSpanner.next(60); // return 1
27 * stockSpanner.next(70); // return 2
28 * stockSpanner.next(60); // return 1
29 * 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.
30 * stockSpanner.next(85); // return 6
31 *
32 *
33 * Constraints:
34 *
35 * 1 <= price <= 10^5
36 * At most 10^4 calls will be made to next.
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/online-stock-span/
42// discuss: https://leetcode.com/problems/online-stock-span/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46struct StockSpanner {
47 vec![]
48 }
49
50
51/**
52 * `&self` means the method takes an immutable reference.
53 * If you need a mutable reference, change it to `&mut self` instead.
54 */
55impl StockSpanner {
56
57 fn new() -> Self {
58
59 }
60
61 fn next(&self, price: i32) -> i32 {
62
63 }
64}
65
66/**
67 * Your StockSpanner object will be instantiated and called as such:
68 * let obj = StockSpanner::new();
69 * let ret_1: i32 = obj.next(price);
70 */
71
72// submission codes end
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn test_901() {
80 }
81}
82
Back
© 2025 bowen.ge All Rights Reserved.