Min Stack Leetcode | Get Min at pop | solution stack Hindi Explained | Data structure & Algorithms

preview_player
Показать описание
This is the video under the series of DATA STRUCTURE & ALGORITHM in a STACK Playlist. Now we are going to solve a stack problem Min Stack or Get min at pop from GeeksForGeeks.

----------------------------------------------------------------------------------------

► 155. Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Implement the MinStack class:

MinStack() initializes the stack object.
void push(int val) pushes the element val onto the stack.
void pop() removes the element on the top of the stack.
int top() gets the top element of the stack.
int getMin() retrieves the minimum element in the stack.

Input
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

Output
[null,null,null,null,-3,null,0,-2]

Explanation
MinStack minStack = new MinStack();

----------------------------------------------------------------------------------------

*Follow me *

----------------------------------------------------------------------------------------

►Our Playlists on:-

------------------------------------------------------------------------

🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟

✨ Tags ✨
leetcode problems
leetcode problems java
leetcode problems python
leetcode problems that got me tired
leetcode problems c++
leetcode problems and solutions python
leetcode problems playlist
leetcode problems and solutions java
leetcode problems in Hindi
leetcode problems javascript
leetcode problems and solutions
leetcode problems of the day
leetcode problems for beginners
leetcode problems easy
leetcode problems js
Introduction to the graph data structure
stack practice problems
stack practice problems gfg
leetcode stack questions
leetcode stack queue
stack hello world
MIn stack leetcode solution
Get Min at pop gfg
remove consecutive duplicates from a string
question asked in Google
off-campus placement
number of closed islands
Practice stack data structure
Stack in a data structure in Hindi
Stack Full playlist for Beginners
algorithms
graph
data structure
sorting algorithms
algorithm analysis
gate computer science preparation
programming languages

#stack #Leetcode #DSA
Рекомендации по теме
Комментарии
Автор

Bohht achhe se explain kiya aapne
Koi aise nhi krata
Jo Aise hme btaye ke abhi ruko socho...
And apki language 👏🏻 ittni beginner friendly 🙏🏻🙏🏻
Aapki speed, video quality, sound quality, apke explain krne ka way 🔥🔥just splendid👌🏻👌🏻

abhaykumargoyal
Автор

Glad you share this point at 12:30 as while doing Valid Parentheses i did the same mistake by reversing the order in condition, later i realised that this actually make difference lol

vitaminprotein
Автор

i got this in first attempt because you said to attempt, it really helped, i have completed this question without any help .

Kartik_Mitt
Автор

Your explanation was excellent. I had been stuck on this code for the past two days and watched numerous videos, but you covered everything in detail, including small points like void and int.😊😊Thank you so much.

ashishdeepkaur
Автор

You explained it amazingly brother, but now this question has become a medium level question in leetcode and many more testcases are added where they are also testing multiple occurrences of possible min element. So an upgraded solution with same logic with a added frequency hashmap will look like this

class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
self.min_freq_map = {}

def push(self, val: int) -> None:
self.stack.append(val)
if self.min_stack:
if val <= self.min_stack[-1]:
if val in self.min_freq_map:
self.min_freq_map[val] += 1
else:
self.min_stack.append(val)
self.min_freq_map[val] = 1
else:
self.min_stack.append(val)
self.min_freq_map[val] = 1

def pop(self) -> None:
x = self.stack.pop()
if self.min_stack[-1] == x:
if self.min_freq_map[x] > 1:
self.min_freq_map[x] -= 1
else:
self.min_freq_map.pop(x)
self.min_stack.pop()

def top(self) -> int:
return self.stack[-1]

def getMin(self) -> int:
return self.min_stack[-1]

pavanjain
Автор

The way you tell these small important things is really helpful. Really nice explanation !!!!

ritwikchawla
Автор

the way you explain the question as well as the solution is really amazing. love to see your videos.☺

vaishalisharma
Автор

hn adha tak soch liya tha maine mja aagya bhaiyaji

rahulsati
Автор

Dil jeet liya bhaiya thank you from bottom of my heart.

aryansinha
Автор

TQ very much bhaiya ❤, before this video i have wasted my 2 hours, but after this video i cracked the answer.😊😊

LAXMI_SAHU
Автор

the explainations of the problem was very good bring more of these explanation on DSA related topics series.

AdityaRajSingh-xygt
Автор

Really very good logic and explanation ❤❤

durgeshkushwaha
Автор

bhaiya badi badhiya smjhaye ek baar m dimag m dhuk gya.Thank you bhaiya

luckilygamer
Автор

bhot ache se ye bata diya apne.... thank

shivanigangwar
Автор

Legendary explanation. Thank u bhaiya.

SaumyaSharma
Автор

Can someone explain🚩🚩📗 When I'm writing the same code in JAVA, it's not working. But When I'm changing the pop() method to:
public void pop() {
if(st.peek() == getMin()) min_st.pop(); // *Using getMin() instead of min_st.peek() works* ???

st.pop();
}
//Please explain why writing min_st.peek() doesn't work?

krishnasingh
Автор

8:22 what if we popped 18 then 18 will pop out of stack but will remain in min_stack and then after that we pop 15 then that 15 will get removed from both stack and min_stack and now if we ask for min_element then now in min_stack there is 18 reamining but it was already popped out of main stack so how does this work?
or what if we pop 15 then pop 18 then ask for get_Minvalue then what min value will be retuerend?? since min_Stack is empty?
Am i being dumb or what i wrote do makes sense?

how to fix this?? i think to fix this the min_stack should be as same size as the main_stack so that min_stack(as in use sorted version of stack in descending order) is empty case won't occur and when you pop from main_stack it should be popped from min_stack as well i guess. but this can never be O(1)

Fe-ironman
Автор

Really appreciated your effort bhaiya ❤ even a noob can understand very well from your video bhaiya 😊

SwetaYadav-irlk
Автор

i have maintain double ended queue and time complexity of getting min element is O(1)

naikajsevak
Автор

My Approach :


stack<int> stack;
map<int, int> mpp;
MinStack() {
int c=0;
}

void push(int val) {
stack.push(val);
mpp[val]++;
}

void pop() {
mpp[stack.top()]--;
if(mpp[stack.top()]==0)
mpp.erase(stack.top());
stack.pop();
}

int top() {
return stack.top();
}

int getMin() {
return mpp.begin()->first;
}
};

deepanshjohri