A monotonic stack
Project description
Monotonic Stack
The monotonic increasing stack and monotonic decreasing stack, namely monotonic stack, is a very powerful tool for finding next greater/smaller element. More specifically, always use monotonic increasing stack when we are trying to find the next smaller element, vice versa. The typical usage of monotonic stack is to find the next greater/smaller element in an array.
pip install monotonic_stack
This monotonic stack:
- works with any Python sequence, not just strings, if the items are hashable
- is implemented in pure Python
PyPi: https://pypi.org/project/monotonic-stack/
Usage
>>> from monotonic_stack import MonotonicStack
>>> MS = MonotonicStack([1, 2, 3, 4, 5])
>>> MS.next_greater_on_right()
[2, 3, 4, 5, -1]
>>> MS.next_greater_on_left()
[-1, 1, 2, 3, 4]
>>> MS.next_smaller_on_right()
[-1, -1, -1, -1, -1]
>>> MS.next_smaller_on_left()
[-1, -1, -1, -1, -1]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
monotonic_stack-0.1.1.tar.gz
(2.2 kB
view hashes)
Built Distribution
Close
Hashes for monotonic_stack-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 070bfddc387e9ec1615962d2ee745d1aa09f82af63285db13eb9f8be608d8715 |
|
MD5 | 332fb2b3f67dbfba1dcb3de0c8d671d2 |
|
BLAKE2b-256 | 7d3f477e4cafed899a0becfbcf0abac624203875a0c290e28b13e7bd7af09500 |