### General - if something is monotonic, it is always increasing or decreasing - personal note: especially useful for problems that require you to use multiple max or min contenders from historical data - unlike only a single max or min ### Algorithms - a monotonic [[Stacks|Stack]] or [[Queues|Queue]] is one whose elements are always sorted - could be ascending or descending - we maintain their sorted property by removing elements that would violate the property before adding new elements - for example: array of 1, 5, 8, 15, 23, if you were to push 14 to the stack, you need to first pop the 15 and 23 - final result would be 1, 5, 8, 14 ``` stack = [] for num in nums: while stack.length > 0 AND stack.top >= num: stack.pop() // Between the above and below lines, do some logic depending on the problem stack.push(num) ``` - this monotonic storage is useful in problems where: - you have to find the "next" element based on some criteria - i.e the next greater element - you have a window of elements, & you want to maintain knowledge of the max or min element as the window changes