• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
January 23, 2024 |940 Views
SDE Sheet - Get minimum element from stack
  Share  1 Like
Description
Discussion

This video is part of Stack and Queue section under GFG SDE Sheet.

In this problem, we are given N elements and your task is to Implement a Stack in which you can get a minimum element in O(1) time.

Example :

Input:
push(2)
push(3)
pop()
getMin()
push(1)
getMin()
Output: 2 1

Explanation: In the first test case for query push(2)  Insert 2 into the stack. The stack will be {2}push(3)  Insert 3 into the stack. The stack will be {2 3} pop()    Remove top element from stack Poped element will be 3 the stack will be {2} getMin() Return the minimum element
min element will be 2 push(1)  Insert 1 into the stack. The stack will be {2 1} getMin() Return the minimum element min element will be 1

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-
Article: https://www.geeksforgeeks.org/design-a-stack-that-supports-getmin-in-o1-time-and-o1-extra-space/
Problem: https://www.geeksforgeeks.org/problems/get-minimum-element-from-stack/1
SDE Sheet Link: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More