Skip to main content

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


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)

Uploaded Source

Built Distribution

monotonic_stack-0.1.1-py2.py3-none-any.whl (2.9 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page