901. 股票价格跨度  

  描述:
    leetcode刷题总结901-950

 

 

   思路:单调栈。

    

class StockSpanner {
    Stack<Integer> prices, weights;

    public StockSpanner() {
        prices = new Stack();
        weights = new Stack();
    }

    public int next(int price) {
        int w = 1;
        while (!prices.isEmpty() && prices.peek() <= price) {
            prices.pop();
            w += weights.pop();
        }

        prices.push(price);
        weights.push(w);
        return w;
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
  • 2021-08-19
  • 2022-01-30
  • 2021-08-10
  • 2021-05-23
猜你喜欢
  • 2021-07-02
  • 2022-12-23
  • 2021-04-06
  • 2021-12-17
  • 2021-06-28
  • 2022-12-23
  • 2021-12-15
相关资源
相似解决方案