Implement Queue using Stacks | Amortised O(1) | Leetcode 232 | Google

preview_player
Показать описание
This is the 5th Video of our Playlist "Stack  : Popular Interview Problems".In this video we will try to solve a very good and famous Stack problem.
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases. 
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Implement Queue using Stacks | Amortised O(1) | Leetcode 232 | Google
Company Tags  : Meta, Amazon, Netflix, Apple, Google, Amazon

Approach Summary : The solution has methods to perform common queue operations: push(int x): Adds an element to the queue. If the input stack is empty, it sets the peekEl (peek element) to the newly added element. pop(): Removes and returns the front element of the queue. If the output stack is empty, it transfers elements from the input stack to the output stack to reverse their order. The time complexity for the pop operation is amortized O(1). peek(): Returns the front element of the queue without removing it. If the output stack is empty, it returns the precomputed peekEl. empty(): Checks if the queue is empty by verifying both the input and output stacks are empty. The implementation optimizes the pop operation by transferring elements between the two stacks only when necessary, resulting in amortized constant time complexity for the pop operation.

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

 Timelines
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #2024 #newyear
Рекомендации по теме
Комментарии
Автор

I always take this suggestion seriously. Never underestimate Easy Qns
Thanks

souravjoshi
Автор

As expected, Your videos are always worth watching

Coding
Автор

I admired of your teaching way. Please also cover tree, graph and dp questions.

manishprajapati
Автор

i have solved it with 1 stack, pushing ele to the bottom of stack every time push is called.
push() - On
peek() - O1
pop() - O1

bhuppidhamii
Автор

I learned something new again. Thanks a lot 😊

nagmakhan
Автор

did it on my own using brute force....came here for optimization...
and literally the explanation was just 💖💖
POTD DONE [29.1.24]✅✅

oqant
Автор

Had done this question previously by watching some other video no discredits to them but I didn't understand that well so had to kind of memorize but watching this the whole logic became so easy and clear to me. MIK sir your explanations are gold <3. Also Congratulations for 17k and hoping to see 50k very soon.

Sawlf
Автор

this was my approach to solve the problem:
class MyQueue {
Stack<Integer> input;
Stack<Integer> output;

public MyQueue() {
input = new Stack<Integer>();
output = new Stack<Integer>();
}

public void push(int x) {
input.push(x);
}

public int pop() {
if (!output.empty()) {
return output.pop();
}

while (!input.empty()) {
output.push(input.pop());
}
return output.pop();
}

public int peek() {
if (!output.empty()) {
return output.peek();
}

while (!input.empty()) {
output.push(input.pop());
}
return output.peek();
}

public boolean empty() {
return output.size() == 0 && input.size() == 0;
}
}
always commenting for better reach🔥

piyushacharya
Автор

Really thankyou bhaiya for your explanation bhaiya you always teaches us something new and also give valuable lesson that don't run after solving easy hard question solve those questions by which we can learn lot of things ❤️❤️🙏

udaytewary
Автор

To my surprise I solved this on my own.

molyoxide
Автор

Dude, really useful and learn something new.

zaviyakhurshid
Автор

Excuse me sir one doubt do i have to implement stack without using stl during interview?

ektuhaso
Автор

at 2:20 if you push the element to the front then the queue should be 2, 1 not 1, 2 why did you do that

deepikakala
Автор

class MyQueue {
Stack<Integer >s1=new Stack<>();
Stack<Integer>s2=new Stack<>();
public MyQueue(){
}
public void push(int x){
while(!s1.empty()){
s2.push(s1.pop());
}
s1.push(x);
while(!s2.empty())
s1.push(s2.pop());
}
public int pop(){
return s1.pop();
}
public int peek(){
return s1.peek();
}
public boolean empty(){
return s1.empty();
}
}
please make video on leetcode weekly contest qn.no.4 bitmagic🎉❤

dayashankarlakhotia