Monotonic Stack: Introduction and Applications in Data Structures


6 min read 07-11-2024
Monotonic Stack: Introduction and Applications in Data Structures

Introduction

In the realm of data structures and algorithms, the concept of a monotonic stack stands as a powerful and versatile tool. Its unique characteristics and applications have made it a staple in the arsenals of programmers and software engineers. At its core, a monotonic stack is a stack data structure where the elements are arranged in a strictly increasing or decreasing order. This inherent property allows for efficient processing of various computational tasks, ranging from finding the next greater element in an array to solving problems related to histograms and stock prices.

Understanding Monotonic Stacks

To grasp the essence of a monotonic stack, let's first delve into the fundamentals of a stack. A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. Imagine a stack of plates, where you can only add or remove plates from the top. The last plate you put on is the first one you take off.

Now, let's introduce the monotonic constraint. A monotonic stack ensures that either all the elements in the stack are in strictly increasing order or strictly decreasing order. For instance, a monotonically increasing stack would have its elements arranged from smallest to largest, while a monotonically decreasing stack would have its elements arranged from largest to smallest.

Why Use Monotonic Stacks?

The key advantage of using a monotonic stack lies in its ability to efficiently track and retrieve elements based on their relative values. This property is particularly useful in scenarios where we need to find the nearest element that satisfies a specific condition, such as being greater than or less than a given element.

Applications of Monotonic Stacks

Monotonic stacks have a wide range of applications in various fields, including:

1. Finding the Next Greater Element

A classic application of monotonic stacks is finding the next greater element for each element in a given array. The next greater element of an element is the first element to its right that is greater than it.

Algorithm:

  1. Initialize an empty monotonic stack.
  2. Iterate through the array from left to right.
  3. For each element, pop elements from the stack until you find an element greater than or equal to the current element.
  4. If you find an element greater than the current element, that element is the next greater element for the current element.
  5. Push the current element onto the stack.
  6. Repeat steps 3-5 for all elements in the array.

Example:

Let's consider the array [2, 1, 5, 6, 2, 3]. Using a monotonic stack, we can find the next greater element for each element:

Element Next Greater Element
2 5
1 5
5 6
6 -1
2 3
3 -1

2. Finding the Largest Rectangular Area in a Histogram

Another prominent application of monotonic stacks is finding the largest rectangular area in a histogram. A histogram is a graphical representation of data using bars of different heights. The largest rectangular area is the maximum area that can be enclosed within the histogram bars.

Algorithm:

  1. Initialize an empty monotonic stack.
  2. Iterate through the histogram bars from left to right.
  3. For each bar, pop elements from the stack until you find a bar shorter than or equal to the current bar.
  4. Calculate the area of the rectangle formed by the popped bars and the current bar.
  5. Update the maximum area encountered so far.
  6. Push the current bar onto the stack.
  7. Repeat steps 3-6 for all bars in the histogram.

Example:

Consider a histogram with bars of heights [2, 1, 5, 6, 2, 3]. Using a monotonic stack, we can find the largest rectangular area:

| Bar | Height |
|---|---|
| 1 | 2 |
| 2 | 1 |
| 3 | 5 |
| 4 | 6 |
| 5 | 2 |
| 6 | 3 |

The largest rectangular area is formed by the bars with heights 5, 6, and 2. The area is 6 * 3 = 18.

3. Stock Span Problem

The stock span problem involves finding the maximum number of consecutive days for which the stock price on a given day was less than or equal to the stock price on the current day.

Algorithm:

  1. Initialize an empty monotonic stack.
  2. Iterate through the stock prices from left to right.
  3. For each price, pop elements from the stack until you find a price greater than or equal to the current price.
  4. The number of elements popped from the stack represents the stock span for the current price.
  5. Push the current price onto the stack.
  6. Repeat steps 3-5 for all stock prices.

Example:

Suppose the stock prices for the last six days are [100, 80, 60, 70, 60, 75]. Using a monotonic stack, we can find the stock span for each day:

Day Price Stock Span
1 100 1
2 80 1
3 60 1
4 70 4
5 60 1
6 75 6

4. Other Applications

Monotonic stacks find applications in various other scenarios, including:

  • Finding the largest element in a sliding window: A sliding window is a subarray of fixed size that moves along the main array. Monotonic stacks can efficiently find the largest element within each sliding window.

  • Processing queries related to ranges: In problems involving queries on ranges of elements, monotonic stacks can be used to determine the maximum or minimum element within a specified range.

  • Optimizing algorithms: In some algorithms, using a monotonic stack can optimize the computational complexity, leading to more efficient solutions.

Advantages of Using Monotonic Stacks

  • Efficiency: Monotonic stacks offer efficient solutions for various problems, often reducing the time complexity to O(n), where n is the size of the input.

  • Simplicity: The concept of monotonic stacks is relatively simple to understand and implement.

  • Versatility: Monotonic stacks have a wide range of applications, making them a versatile tool for programmers.

Implementation in Different Programming Languages

Monotonic stacks can be implemented in various programming languages, including C++, Python, and Java. The implementation involves creating a stack data structure and incorporating the monotonic property.

C++ Implementation:

#include <stack>
#include <vector>

std::vector<int> nextGreaterElement(const std::vector<int>& arr) {
  std::vector<int> result(arr.size(), -1);
  std::stack<int> st;

  for (int i = 0; i < arr.size(); ++i) {
    while (!st.empty() && arr[st.top()] < arr[i]) {
      result[st.top()] = arr[i];
      st.pop();
    }
    st.push(i);
  }
  return result;
}

Python Implementation:

def next_greater_element(arr):
  result = [-1] * len(arr)
  stack = []

  for i in range(len(arr)):
    while stack and arr[stack[-1]] < arr[i]:
      result[stack.pop()] = arr[i]
    stack.append(i)

  return result

Java Implementation:

import java.util.Stack;
import java.util.Vector;

public class MonotonicStack {

  public static Vector<Integer> nextGreaterElement(Vector<Integer> arr) {
    Vector<Integer> result = new Vector<>(arr.size());
    result.addAll(Collections.nCopies(arr.size(), -1));
    Stack<Integer> st = new Stack<>();

    for (int i = 0; i < arr.size(); ++i) {
      while (!st.isEmpty() && arr.get(st.peek()) < arr.get(i)) {
        result.set(st.pop(), arr.get(i));
      }
      st.push(i);
    }
    return result;
  }
}

Conclusion

Monotonic stacks are a powerful and elegant data structure that offers efficient solutions for various computational tasks. Their ability to track and retrieve elements based on their relative values makes them a valuable tool in areas like finding the next greater element, calculating the largest rectangular area in a histogram, and solving the stock span problem. Understanding the concept of monotonic stacks and their applications can significantly enhance your problem-solving skills in data structures and algorithms.

FAQs

1. What is the time complexity of operations on a monotonic stack?

The time complexity of most operations on a monotonic stack, such as push, pop, and top, is O(1) on average. However, in the worst case, where we need to pop all elements from the stack, the time complexity can be O(n), where n is the number of elements in the stack.

2. How is a monotonic stack different from a regular stack?

A regular stack follows the LIFO principle and allows elements to be pushed and popped in any order. However, a monotonic stack imposes the additional constraint that the elements are arranged in a strictly increasing or decreasing order.

3. Can a monotonic stack be used to find the next smaller element?

Yes, a monotonic stack can be used to find the next smaller element in a similar way to finding the next greater element. You just need to modify the comparison operation in the algorithm.

4. What are some real-world applications of monotonic stacks?

Monotonic stacks have applications in various domains, including:

  • Financial analysis: For tasks like stock price prediction and analyzing trends.
  • Image processing: For tasks like finding the largest connected component in a binary image.
  • Game development: For tasks like pathfinding and collision detection.

5. Can a monotonic stack be implemented using other data structures?

Yes, a monotonic stack can be implemented using other data structures like arrays or linked lists. However, using a dedicated stack data structure is usually more convenient and efficient due to its inherent LIFO behavior.