Efficient Rolling Max and Min Window

前端 未结 5 1436
没有蜡笔的小新
没有蜡笔的小新 2021-02-14 06:41

I want to calculate a rolling maximum and minimum value efficiently. Meaning anything better than recalculating the maximum/minimum from all the values in use every time the win

5条回答
  •  天涯浪人
    2021-02-14 07:22

    I would suggest maintaining a stack which supports getMin() or getMax().

    This can be done with two stacks and costs only constant time.

    fyi: https://www.geeksforgeeks.org/design-a-stack-that-supports-getmin-in-o1-time-and-o1-extra-space/

提交回复
热议问题