Monotonic Stack in Python
A Monotonic Stack is a data structure used to maintain elements in a monotonically increasing or decreasing order. It's particularly useful in problems like finding the next or previous greater or smaller elements. In this article, we'll learn how to implement and use a monotonic stack in Python with examples.
Types of Monotonic Stack :
- Monotonic Increasing Stack
- Monotonic Decreasing Stack
Monotonic Increasing Stack :
A Monotonic Increasing Stack is a stack where elements are placed in increasing order from the bottom to the top. If a new element is smaller than the current top element, elements are popped from the stack until the stack maintains its increasing order.
Algorithm:
- Initialize an empty stack.
- Iterate through the elements.
- For each element, pop elements from the stack while the stack is not empty and top of the stack is greater than the current element.
- Push the current element onto the stack.
- The stack will then contain elements in a monotonic increasing order.
Below is the implementation of the above approach :
def monotonic_increasing_stack(arr):
# Initialize an empty stack to maintain the increasing order
stack = []
# Iterate through each element in the input array
for num in arr:
# While the stack is not empty and the top of the stack is greater than the current element
while stack and stack[-1] > num:
# Pop the top element from the stack
stack.pop()
# Push the current element onto the stack
stack.append(num)
# Return the stack, which now contains elements in monotonic increasing order
return stack
# Example usage
arr = [2, 1, 2, 4, 3]
print(monotonic_increasing_stack(arr))
Output
[1, 2, 3]
Time Complexity : O(n), where n is the number of elements in array
Auxiliary Space : O(n)
Monotonic Decreasing Stack :
A Monotonic Decreasing Stack is a stack where elements are placed in decreasing order from the bottom to the top. If a new element is greater than the current top element, elements are popped from the stack until the stack maintains its decreasing order.
Algorithm :
- Start with an empty stack.
- Iterate through the elements of the input array.
- For each element, pop elements from the while the stack is not empty and the top of the stack is less than the current element.
- Push the current element onto the stack.
- The stack will then contain elements in a monotonic decreasing order.
Below is the implementation of the above approach:
def monotonic_decreasing_stack(arr):
# Initialize an empty stack to maintain the decreasing order
stack = []
# Iterate through each element in the input array
for num in arr:
# While the stack is not empty and the top of the stack is less than the current element
while stack and stack[-1] < num:
# Pop the top element from the stack
stack.pop()
# Push the current element onto the stack
stack.append(num)
# Return the stack, which now contains elements in monotonic decreasing order
return stack
# Example usage
arr = [2, 1, 2, 4, 3]
print(monotonic_decreasing_stack(arr))
Output
[4, 3]
Time Complexity : O(n), where n is the number of elements in array
Auxiliary Space : O(n), where n is the number of elements in array
Related articles: