Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8777719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:20:43+00:00 2026-06-13T19:20:43+00:00

The reason I’m asking this question is because I cannot see why the way

  • 0

The reason I’m asking this question is because I cannot see why the way I think cannot be applied to this particular question

“How would you design a stack which,
in addition to push and pop, also has a function min which returns the minimum element? Push, pop and min should all operate in O(1) time
“

My basic solution: Wouldn’t it be possible if we had a variable in stack class, that whenever we were pushing an item to stack we would check if it is smaller than our min variable. If it is assign the value to the min, if not ignore.

You would still get the O(1) as the min function would be;

int getMinimum(){
  return min;
}

Why this solution is never mentioned, or what is the fault with the way I think?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:20:45+00:00Added an answer on June 13, 2026 at 7:20 pm

    This wouldn’t work if you popped numbers off the stack.

    Ex. 2,4,5,3,1. After you pop 1 off, what is your minimum?

    The solution is to keep a stack of minimums, not just a single value. If you encounter a value that is less than equal to the current minimum, you need to push it onto the min-stack.

    Ex.

    Push(4):
    Stack: 4
    Min-stack: 4
    
    Push(2):
    Stack: 4 2
    Min-stack: 4 2
    
    Push(2):
    Stack: 4 2 2
    Min-stack: 4 2 2
    
    Push(5):
    Stack: 4 2 2 5
    Min-stack: 4 2 2
    
    Push(3):
    Stack: 4 2 2 5 3
    Min-stack: 4 2 2
    
    Push(1):
    Stack: 4 2 2 5 3 1
    Min-stack: 4 2 2 1
    
    Pop():
    Stack: 4 2 2 5 3
    Min-stack: 4 2 2
    
    Pop():
    Stack: 4 2 2 5
    Min-stack: 4 2 2
    
    Pop():
    Stack: 4 2 2
    Min-stack: 4 2 2
    
    Pop():
    Stack: 4 2
    Min-stack: 4 2
    
    Pop():
    Stack: 4
    Min-stack: 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The reason I am asking this question is because I have landed my first
The reason I am asking this as a separate question to the multitude of
The reason I'm asking this is because there is a validation in OpenERP that
The reason I'm asking this is because right now I use an NSMutableSet of
The reason why I am asking this is because I was recommended by @Greg
The reason why I'm asking this, is because I'm coding in C++, in putty/ssh
The reason for me asking this question is:- I get a real buzz out
The reason I am asking this is due to my observation that frameworks like
The reason I asked this question is that I wanted to create a helper
The reason I ask this question is there is no list available on Internet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.