Implement Queue using Stacks | Queue in Data Structure & Algorithms | Interview Question Hello World
Vložit
- čas přidán 7. 09. 2024
- This is the video under the series of DATA STRUCTURE & ALGORITHM in a STACK & QUEUE Playlist. Now we are going to Learn to solve Questions on Queue in Data Structure from Leetcode Implement Queue using Stacks.
Join My Telegram channel for more Updates: telegram.me/he...
complete DSA preparation: github.com/Pri...
---------------------------------------------------------------------------------------
► Implement Queue using Stacks: leetcode.com/p...
► code In this video: github.com/Pri...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
Telegram► telegram.me/he...
----------------------------------------------------------------------------------------
►Our Playlists on:-
► Tree: • Tree Data Structure & ...
► Stack & Queue: • Stack & Queue Data Str...
► Hashing: • Hashing Data Structure...
► Graph: • Graph Data Structure &...
► Matrix: • Matrix (Multidimension...
► STL: • Standard Template Libr...
► Leetcode: • LeetCode Solutions And...
►Competitive Programming: • Full course in Competi...
►C++ Full Course : • C++ full Course in HINDI
►Algorithms: • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
🌟 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 queue data structure
Queue playlist Hello world
Queue practice problems
Queue practice problems gfg
leetcode Queue questions
leetcode Queue queue
Queue hello world
Types of Queue in Data structure & algorithms
playlist Queue Hindi
question asked in Google
off-campus placement
Practice Queue data structure
Queue in a data structure in Hindi
Queue Full playlist for Beginners
algorithms
graph
data structure
sorting algorithms
algorithm analysis
gate computer science preparation
programming languages
#Queue #Leetcode #programming
class MyQueue {
stackinput,output;
// when doing peek or pop we have to fill the elements in a different container (so we will use two stacks for this) as in stack the element that should be on front in queue are at the last
void transfer() {
if (output.empty()) {
while (!input.empty()) {
output.push(input.top());
input.pop();
}
}
}
public:
MyQueue() {}
void push(int x) {
input.push(x);
}
int pop() {
transfer();
int front = output.top();
output.pop();
return front;
}
int peek() {
transfer();
return output.top();
}
bool empty() {
return input.empty()&& output.empty();
}
};
Beautifully explained Prince bhai , endsem chal raha tha toh consistency thoda break ho gaya mera
My code :
class MyQueue {
public:
stack input, output;
MyQueue() {
}
void push(int x) {
input.push(x);
}
int pop() {
int ans = peek();
output.pop();
return ans;
}
int peek() {
if (output.empty() == true){
while (!input.empty()){
output.push(input.top());
input.pop();
}
}
return output.top();
}
bool empty() {
if (input.empty() == true and output.empty() == true){
return true;
}
return false;
}
};
koi na bro aap bhut aacha kar rhe ho
there is catch
input.push(10);
input.push(20);
input.push(30);
input.push(40);
if i you call pop() method then it should return 10 but in your code it will return 40; if you never call peek() method before calling pop() method
waooo
@@HelloWorldbyprince wow kya hota hai master ji, push k time output mein dal dijiye .
class MyQueue {
public:
stack s1,s2;
MyQueue() {
}
void push(int x) {
s2.push(x);
}
int pop() {
if(s1.empty())
{
while(!s2.empty())
{
s1.push(s2.top());
s2.pop();
}
}
int v=s1.top();
s1.pop();
return v;
}
int peek() {
if(s1.empty())
{
while(!s2.empty())
{
s1.push(s2.top());
s2.pop();
}
}
return s1.top();
}
bool empty() {
return s1.empty()&&s2.empty();
}
};
shayad yeh a66a hai thoda
sir 318 virtual contest ke questions pe video baniyye . it will be really helpful
1st method implemented successfully bro
nice work
Done in one go.
ye pop vale operation me apne peek () function ko call kiya hai kya niche vala
Great video......Thank you.
Thanks a lot please share my channel with your friends
@@HelloWorldbyprince sure ...i will.
Great video 👍
o(n) vala coded myself
Waooo yaar good job
aaya. same thaught
very nice!!
Soch liya bhaiya pehle wala method
nice yaar good
i will try it
sure
Sir, I tried
Thanks a lot
nice video!
Thank you! Cheers!
i think this solution but fell lazy to implement
Bhai ye jo last me solution btaya iski complexity bhi toh O(N) aaegi?
Kyuki peek me jab dusra queue empty hoga toh poora first queue khali krke dusre me daalna padh rha
Bhai Video to theek h , but leetcode ka logo kahi or se inspired lag raha h
@@whoawhoa2764 😄
😂😂 bhut tej ho bhai tum
@@HelloWorldbyprince Bas aapse kam
Prince bhai complexity iski bhi O(N) hogai kya?
ha shayad
Nhi socha maine...Meri galti I should have invested more time
koi baat nhi bro dhere dhere insaan sikhta hai
mera bhi run kar gya ...answer sahi aa gya but koi test case fail ho gya tha.
kon sa test case fail ho gya brother
Showing TLE , code not working class MyQueue {
public:
stack input, output;
MyQueue() {
}
void push(int x)
{
input.push(x);
}
int pop()
{
int val =output.top();
output.pop();
return val;
}
int peek()
{
if(output.empty())
{
while(input.empty()==false)
output.push(input.top());
input.pop();
}
return output.top();
}
bool empty()
{
return input.empty() && output.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
aagya
Waoo nice
@@HelloWorldbyprince love you bro
maine second solution soch liya tha.....but implementation me problem ho rahi thi
koi baat nahi
dhere dhere time ke sath samjh me aa jayega