LeetCode-Online Stock Span

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24133491/article/details/82557579

Description:
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.

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.

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:

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:

  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.

题意:给定一个一维数组,表示每天的股票价格,要求找出一个跨度,定义为从当天开始的前面的连续天数中股票价格不大于当天股票的天数;要注意的是测试用例中给出的数组中的值可能为null;

解法一(超时):最简单的办法就是把每次的价格存入到List中,从当前位置往前找连续天数中股票价格不大于当前的天数;但是时间复杂度太高;

class StockSpanner {
    private List<Integer> list;
    private int len;

    public StockSpanner() {
        this.list = new ArrayList<>();
        this.len = 0;
    }

    public int next(int price) {
        this.list.add(price);
        this.len++;
        int max = 0;
        for (int i = this.len - 1; i >= 0; i--) {
            if (list.get(i) > price) break;
            max++;
        }
        return max;
    }
}
/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner obj = new StockSpanner();
 * int param_1 = obj.next(price);
 */

解法二:我们可以利用两个List,一个用于保存股票的价格,另外一个用于保存股票的跨度;因此,对于每天出现的新的股票价格,只要前一个股票价格不大于当前股票价格,我们就可以加上上一个股票价格的跨度,此后再次判断减去跨度之后的那天的价格是否不大于当前股票价格…重复这个操作知道往前不再有股票价格或者股票价格大于当前股票的价格;

class StockSpanner {
    private List<Integer> listPrice;
    private int len;
    private List<Integer> listPriceSpan;

    public StockSpanner() {
        this.listPrice = new ArrayList<>();
        this.listPriceSpan = new ArrayList<>();
        this.len = 0;
    }

    public int next(int price) {
        if ((Integer)price == null) {
            throw new NullPointerException("is not a integer value");
        }
        int span = 1;
        int index = this.len - 1;
        while (index >= 0 && this.listPrice.get(index) <= price) {
            span += this.listPriceSpan.get(index);
            index -= this.listPriceSpan.get(index);
        }
        this.listPrice.add(price);
        this.listPriceSpan.add(span);
        this.len++;
        return span;
    }
}

/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner obj = new StockSpanner();
 * int param_1 = obj.next(price);
 */

猜你喜欢

转载自blog.csdn.net/qq_24133491/article/details/82557579