Monotonic Stack in Python Last Updated : 30 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 StackMonotonic Decreasing StackMonotonic 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 : Python 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 arrayAuxiliary 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: Python 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 arrayAuxiliary Space : O(n), where n is the number of elements in arrayRelated articles: Introduction to monotonic stackHow to Identify and Solve Monotonic Stack Problems? Comment More infoAdvertise with us Next Article numpy.stack() in Python A abhaystriver Follow Improve Article Tags : Stack Python Python-DSA Practice Tags : pythonStack Similar Reads Stack in Python A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner. In stack, a new element is added at one end and an element is removed from that end only. The insert and delete operations are often called push and pop. The functions associated wi 8 min read numpy.stack() in Python NumPy is a famous Python library used for working with arrays. One of the important functions of this library is stack(). Important points:stack() is used for joining multiple NumPy arrays. Unlike, concatenate(), it joins arrays along a new axis. It returns a NumPy array.to join 2 arrays, they must 6 min read numpy.vstack() in python numpy.vstack() is a function in NumPy used to stack arrays vertically (row-wise). It takes a sequence of arrays as input and returns a single array by stacking them along the vertical axis (axis 0).Example: Vertical Stacking of 1D Arrays Using numpy.vstack()Pythonimport numpy as geek a = geek.array( 2 min read numpy.ma.row_stack() in Python numpy.ma.row_stack() : This function helps stacking arrays row wise in sequence vertically manner. Parameters : tup : sequence of ndarrays. 1D arrays must have same length, arrays must have the same shape along with all the axis. Result : Row-wise stacked arrays Code #1: Explaining row_stack() Pytho 1 min read Matplotlib.pyplot.stackplot() in Python Matplotlib is a visualization library available in Python. Pyplot contains various functions that help matplotlib behave like MATLAB. It is used as matplotlib.pyplot for plotting figures, creating areas, lines, etc. Stackplot Among so many functions provided by pyplot one is stackplot which will be 3 min read numpy.column_stack() in Python numpy.column_stack() function is used to stack 1-D arrays as columns into a 2-D array.It takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. 2-D arrays are stacked as-is, just like with hstack function. Syntax : numpy.column_stack(tup) Parameters : tup : [sequence of 2 min read Like