Monotonic Stack

Problems used for

We always go through an array from left to right

What is a monotonic stack?

  1. Strictly increasing - Every element is strictly greater than the previous - [1, 4, 5, 8, 9]
  2. Non-decreasing - Every element is greater or equal than the previous - [1, 4, 5, 5, 8, 9, 9]
  3. Strictly decreasing - Every element is strictly smaller than the previous - [9, 8, 5, 4, 1]
  4. Non-increasing - Every element is smaller or equal than the previous - [9, 9, 8, 5, 5, 4, 1]

The right most element is at the top, the left most at the bottom of the stack.

How does the function we build work?

What is the relationship between problem statement and stack?

The relation is inverse:

  1. Greater -> Decreasing stack
  2. Smaller -> Increasing stack

How can we find out which stack we have to use?

Finding the next greater element means that the element at the top of the stack has to be smaller than the current element

Questions

Terms

References

739 Daily Temperatures