[ALGO][DS] Monotonic Stack

Monotonic Stack

May 15, 2023 - 1 minute read -
DS

When to use Monotonic Stack

Monotonic Stack helps you find the next greater or the next smaller element. It has the following characteristics.

  • it is often used to solve “range queries in an array” problem
  • maintain maximum and minimum elements in the range
  • keep the order of elements in range
  • element that have been removed never get used again

Increasing vs Decreasing

  • Use increasing when you are looking for next smaller element
  • Use decreasing when you are looking for next greater element

Sources

  • https://medium.com/techtofreedom/algorithms-for-interview-2-monotonic-stack-462251689da8