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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file monotonic_stack-0.1.1.tar.gz.
File metadata
- Download URL: monotonic_stack-0.1.1.tar.gz
- Upload date:
- Size: 2.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ed50748855f539de73f884391f678832cb9b20a7a36e231e82c0bc9b4da1075
|
|
| MD5 |
ec28ae40bb9d99f099b53dafe4043bd6
|
|
| BLAKE2b-256 |
6f2004eb1db1f9ac0d2db1984f8e04da80dbb20b63d3b05867a183ecc540cb18
|
File details
Details for the file monotonic_stack-0.1.1-py2.py3-none-any.whl.
File metadata
- Download URL: monotonic_stack-0.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 2.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
070bfddc387e9ec1615962d2ee745d1aa09f82af63285db13eb9f8be608d8715
|
|
| MD5 |
332fb2b3f67dbfba1dcb3de0c8d671d2
|
|
| BLAKE2b-256 |
7d3f477e4cafed899a0becfbcf0abac624203875a0c290e28b13e7bd7af09500
|