npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tanishqvyas/min-stack

v1.1.3

Published

A simple min stack implementation.

Downloads

7

Readme

Min Stack

This is a simple implementation of Min Stack. A data structure that supports all the operations of a stack along with an additional method getMin that supports fetching the minimum item's value in the stack in constant time.

Push Operation:

Adds an element to the top of the stack. If the stack is empty, the element becomes both the top element and the minimum element. If the stack is not empty, the minimum element is updated based on the newly added element.

Pop Operation:

Removes the top element from the stack. The minimum element may need to be updated based on the elements remaining in the stack after the pop operation.

Peek Operation:

Returns the value of the top element without removing it from the stack.

Get Minimum Operation:

Returns the minimum element currently stored in the stack. This operation is performed in constant time, making it an efficient way to retrieve the minimum value.

Data Structure:

The min stack typically consists of a regular stack for storing elements and an additional data structure (e.g., an auxiliary stack or a linked list) to keep track of the minimum element at each step.

Time Complexity:

The push, pop, and peek operations have a time complexity of O(1). The get minimum operation also has a time complexity of O(1) since the minimum element is tracked and updated as elements are pushed and popped.

Space Complexity:

The space complexity is O(n), where n is the number of elements in the stack. This includes the space required for both the main stack and the auxiliary data structure used to track the minimum element. The min stack is particularly useful in scenarios where there is a frequent need to find the minimum element in a stack, and efficiency is crucial. It's commonly used in algorithmic and coding interview problems that involve finding the minimum element in a stack in constant time.

Standard Example

import MinStack from "@tanishqvyas/min-stack";

const myStack = new MinStack();

myStack.push(1);
myStack.push(4);
myStack.push(5);
myStack.push(0);

myStack.peek(); // 0

myStack.pop(); // removes 0

myStack.getMin(); // 1

myStack.isEmpty(); // false

myStack.limit; // 1000, default limit

Configuration Options

This stack implementation comes with two config options:

  • limit: The number of elements the stack can accomodate. Default value: 1000

  • cmp: A comparison function that returns the mimimum of two items. Default value: Math.min

import MinStack from "@tanishqvyas/min-stack";

const myStack = new MinStack({
  limit: 100,
  cmp: (a, b) => {
    if (a * a < b * b) {
      return a;
    } else {
      b;
    }
  },
});

// or you can pass these fields partially

const myStackLimited = new MinStack({
  limit: 10,
});

Please feel free to reach out with further improvements on the same by raising a PR at @tanishqvyas/min-stack