Stack & Queue 5: Get minimum element from stack | Must Do Coding Questions | GeeksForGeeks

preview_player
Показать описание
This video contains Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, …
from geeksforgeeks platform.

website link:

Topics covered in this video series are:
1.Arrays
2.String
3.Linked List
4.Stack and Queue
5.Tree and BST
6.Heap
7.Recursion
8.Hashing
9.Graph
10.Greedy
11.Dynamic Programming
12.Divide and Conquer
13.Backtracking
14.Bit Magic

Stack and Queue:
5: Get minimum element from stack :
You are given N elements and your task is to Implement a Stack in which you can get minimum element in O(1) time.

Example 1:
Input:
push(2)
push(3)
pop()
getMin()
push(1)
getMin()
Output: 3 2 1
Explanation: In the first test case for
query
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3 the stack will be {2}
getMin() min element will be 2
push(1) the stack will be {2 1}
getMin() min element will be 1

Code Link:

Intro Music :
Рекомендации по теме
Комментарии
Автор

Sir ur way of explaining is amazing!!
Please continue this series

authorz
Автор

While pushing element in the stack, is there any specific reason for multiplying x with 2? Like, what if we wrote, S.push(x-minEle)? Won't we able to retrieve the previous minElement in that case while popping?

AshAkash
welcome to shbcf.ru