Min Stack (LeetCode 155) | Full Solution with animations using 2 stacks

preview_player
Показать описание


Chapters:
00:00 - Intro
00:37 - Problem Statement
02:46 - A naive approach
04:47 - Efficient Method
09:02 - Dry run of Code
12:16 - Final Thoughts

📚 Links to topics I talk about in the video:

📖 Reference Books:

🎥 My Recording Gear:

💻 Get Social 💻

#leetcode #programming #interview
Рекомендации по теме
Комментарии
Автор

Sir we can also maintain a insertion operation in desending oeder to get minimum inO(1) so space complexity is alsoO(1)
Pls reply🙏🙏🙏

Shiwi
Автор

Hi @Nikhil, You are doing a great job keep it up!
Can you plz make a video on "Sports Analytics" Leet code code problem? which has been asking now days

ManjunathMopagar
Автор

i want task sheduler problem leetcode 621

thalap_-
Автор

Hello @Nikhil Sir, can you please cover some important graph problems i have watched your graph playlist but can you bring some more question related to graph. it will be very helpfull.

amansaxena
Автор

sir can you pls give all leetcode problems in c++

NikkiA-op
Автор

We could also use just one stack and a hashmap to keep track of the min element in the stack
public class MinStack {
private Stack<Integer> stack;
private HashMap<Integer, Integer> minMap;
private int minVal;

public MinStack() {
stack = new Stack<>();
minMap = new HashMap<>();
minVal = Integer.MAX_VALUE;
}

public void push(int x) {
stack.push(x);
minVal = Math.min(minVal, x);
minMap.put(stack.size(), minVal);
}

public int pop() {
int x = stack.pop();
minMap.remove(stack.size() + 1);
if (!stack.isEmpty()) {
minVal = minMap.get(stack.size());
} else {
minVal = Integer.MAX_VALUE;
}
return x;
}

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

public int getMin() {
return minVal;
}
}

praveenkannadiga
Автор

Either we can make a stack of pair datatype and every element of stack contains number, min among all present till top element...

pranavgupta
visit shbcf.ru