Implement O(1) Stack with Push, Pop, Min

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer science, a stack is an abstract data type that serves as a collection of elements, with two main operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. In this challenge, we will implement a stack with push, pop, and min methods running O(1) time.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/stacks_queues("`Stacks Queues`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/PythonStandardLibraryGroup -.-> python/os_system("`Operating System and System`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/stacks_queues -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/conditional_statements -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/tuples -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/function_definition -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/default_arguments -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/importing_modules -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/standard_libraries -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/classes_objects -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/constructor -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/polymorphism -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/encapsulation -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/iterators -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/os_system -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} python/build_in_functions -.-> lab-268888{{"`Implement O(1) Stack with Push, Pop, Min`"}} end

Stack Min

Problem

The problem is to implement a stack with push, pop, and min methods running O(1) time. The push method adds an element to the collection, the pop method removes the most recently added element that was not yet removed, and the min method returns the minimum element in the stack. All three methods should run in constant time, O(1).

Requirements

To solve this problem, we need to consider the following requirements:

  • The stack contains only integers.
  • The input values for push are valid.
  • If we call the min method on an empty stack, we should return sys.maxsize.
  • We can assume that we already have a stack class that can be used for this problem.
  • We can assume that the stack fits in memory.

Example Usage

We can test our implementation with the following scenarios:

  • Push/pop on an empty stack.
  • Push/pop on a non-empty stack.
  • Min on an empty stack.
  • Min on a non-empty stack.

Summary

In summary, we have learned how to implement a stack with push, pop, and min methods running O(1) time. We have also discussed the requirements and example usage of this implementation.

Other Algorithm Tutorials you may like