Тёмный

Implement A Queue Using Stacks - The Queue ADT ("Implement Queue Using Stacks" on LeetCode) 

Back To Back SWE
Подписаться 241 тыс.
Просмотров 42 тыс.
50% 1

Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe....
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Implement a queue (a FIFO structure...first-in-first-out) only using stacks internally as efficiently as possible.
This problem is classic and well known but as always, I want to walk you through the thought process and not just present the solution.
Complexities
n is the total items between the 2 stacks (in the overarching queue)
Time: O( 1 ) - amortized (for enqueue and dequeue operations)
Amortized time is the way to express the time complexity when an algorithm has the very bad time complexity only once in a while besides the time complexity that happens most of time ( / amortized-time-in-the-... .
The motivation for amortized analysis is that looking at the worst-case run time per operation, rather than per algorithm, can be too pessimistic. (en.wikipedia.o....
Space: O( n )
We upper bound space to the maximum amount of items that we will ever store.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: / @hackerrankofficial
Tuschar Roy: / tusharroy2525
GeeksForGeeks: / @geeksforgeeksvideos
Jarvis Johnson: / vsympathyv
Success In Tech: / @successintech
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is number 19.1 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.

Опубликовано:

 

9 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 142   
@BackToBackSWE
@BackToBackSWE 5 лет назад
Table of Contents: The Problem Introduction 0:00 - 0:17 The Queue ADT 0:17 - 1:27 What Problems Does A Stack Give Us? 1:27 - 1:45 Walkthrough With 1 Underlying Stack 1:45 - 6:44 We Missed Something...Go Back 6:44 - 7:39 Walkthrough Using A 2nd Stack 7:39 - 11:19 What You See Is A Queue (Abstraction) 11:19 - 11:52 Space Complexity 11:52 - 12:08 Time Complexity 12:08 - 12:20 Amortized Analysis: What Is It? 12:20 - 13:34 Amortized Analysis: Use For ArrayList In Java 13:34 - 14:05 Wrap Up 14:05 - 14:40 The code for this problem is in the description. Fully commented for teaching purposes.
@BackToBackSWE
@BackToBackSWE 4 года назад
Encapsulation is a means to achieve abstraction
@angelluisgarciaguzman5598
@angelluisgarciaguzman5598 4 года назад
The best part of this is that no code is presented, making it easy to gasp and possible to apply to all known languages without much complication, really appreciate that everything in the vid focused on the key concept of a queue and not jump straight to code.
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@levifoster-grundy240
@levifoster-grundy240 3 года назад
Oh my god you are the only person who made this simple. I been stressing for weeks on how to do it for college. Thank you so much.
@BackToBackSWE
@BackToBackSWE 3 года назад
sure
@cristina-gabrielagavrila8390
@cristina-gabrielagavrila8390 4 года назад
I just discovered your videos and I find them so helpful in my learning process! I deeply appreciate the amount of work you put in each of your videos and hope you pursue your dreams concerning this channel!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@tushar8998
@tushar8998 4 года назад
Thanks a lot. You are the only instructor I have found yet who explains the thinking behind the solution rather than just parroting the solution, like a lot of other instructors do. It is the difference between learning from a person and a book. Thanks for that. I hope that more teachers around the world start to do this.
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@longuyen10119
@longuyen10119 4 года назад
Have been watching your videos for 3 years doing my Comp Sci degree. Thank you so much from your help. You're so much better than most of my lecturers, unfortunately.
@BackToBackSWE
@BackToBackSWE 4 года назад
I've only been doing this since December 2018 haha so I guess 1.5? and nice
@arunkumar777
@arunkumar777 3 года назад
you put lots of effort in your video's bro kudos to that...
@shivamchauhan3562
@shivamchauhan3562 4 года назад
best thing about your videos is you don't feel bored
@BackToBackSWE
@BackToBackSWE 4 года назад
nice
@AlekVila
@AlekVila Год назад
This is a brilliant explanation. So clear!! And now I see a slinky.
@jussaraambrosio
@jussaraambrosio Год назад
Your explanation is amazing! Thanks for this video!!!!
@charan_75
@charan_75 3 года назад
Simple and superior as always.
@abanerjee3704
@abanerjee3704 3 года назад
I like how they nailed the staged rewind part. Good content bro. Keep it up.
@MonisYousuf
@MonisYousuf 3 года назад
Brilliant explanation!!
@BackToBackSWE
@BackToBackSWE 3 года назад
thanks
@b747
@b747 5 лет назад
thanks for the great channel ...you are my new hero! ✌️👍
@BackToBackSWE
@BackToBackSWE 5 лет назад
We are all heroes
@rakeshreddyabbireddy8876
@rakeshreddyabbireddy8876 4 года назад
firstly I feared about dynamic programming but now it is a cakewalk after your watching videos and now queues and stacks also
@BackToBackSWE
@BackToBackSWE 4 года назад
great
@QVL75
@QVL75 2 года назад
Awesome explanation. Thanks!
@yicai7
@yicai7 3 года назад
WHAT A GENIUS!
@jonathanharris2326
@jonathanharris2326 4 года назад
thank you, you're really good at explaining concepts
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@ricardopereira5397
@ricardopereira5397 2 года назад
Great explanation! Thanks for sharing, subscribed and liked the video.
@democratcobra
@democratcobra 3 года назад
Thanks for uploading this...This is very helpfull to me.
@dinohunter7176
@dinohunter7176 2 года назад
Thanks, I love the idea from 7:39. I struggled with tge first approach and it did not made sense how can work O(1)
@helenashatalova7828
@helenashatalova7828 Год назад
Thank you so much for your simple and clear explanation! I can't thank you enough for the work you've done and energy&time you put into making these videos!
@BackToBackSWE
@BackToBackSWE Год назад
Means a lot!
@Voidangular0100
@Voidangular0100 3 года назад
Great explanation
@kraanas
@kraanas 3 года назад
Wow, what a great explanation! It's very easy to understand and interesting to listen :)
@yodkwtf
@yodkwtf 2 года назад
great explaination. Really appreciate it
@ythalorossy
@ythalorossy 5 лет назад
Amazing article. Thank you very much.
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@sahaneakanayaka3394
@sahaneakanayaka3394 3 года назад
Thanks Ben 😄😄
@rkjohn441
@rkjohn441 2 года назад
how such a high quality video!🔥
@anikpait5556
@anikpait5556 4 года назад
Amazing and Clear Explanations! Kudos!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks for watching
@anikpait5556
@anikpait5556 4 года назад
@@BackToBackSWE would love your videos on System Design if possible :)
@logovskii
@logovskii Год назад
best explanation
@BackToBackSWE
@BackToBackSWE Год назад
Thank you 🎉 Please enjoy a special coupon from us - backtobackswe.com/checkout?plan=lifetime-legacy&discount_code=SUB 🚀
@sankalparora9261
@sankalparora9261 4 года назад
You make stuff damn simple. Thank you.
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@trishulsherigar1961
@trishulsherigar1961 3 года назад
icredible explanation..
@BackToBackSWE
@BackToBackSWE 3 года назад
thanks
@fluturehundozi510
@fluturehundozi510 2 года назад
Thank you very much!! The solution from leetcode was a little confusing and I couldn't really understand it, but this video definitely helped me :)
@freesoul2677
@freesoul2677 2 года назад
Thank you so much!
@yosef-alaker
@yosef-alaker Год назад
كل الشكر التقدير لك من متابع عربي ❤❤
@vrashankraom
@vrashankraom 4 года назад
Awesome liked it!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@pranavnyavanandi9710
@pranavnyavanandi9710 2 года назад
Good video man, got to know about what an ADT is - similar to the idea of a java interface, what amortized analysis is, and the solution to the problem.
@unique00imagination
@unique00imagination 2 года назад
amazing explanation, thank you! keep up the great work!
@BackToBackSWE
@BackToBackSWE 2 года назад
Glad to hear that! Subscribe to our DSA course with a flat 30% discount for some amazing content b2bswe.co/3HhvIlV
@tanujatirkey3771
@tanujatirkey3771 3 года назад
You are awesome 👍❣️
@renjieyu8700
@renjieyu8700 5 лет назад
哈哈,谢谢大兄弟清晰的解释😀 爱你
@BackToBackSWE
@BackToBackSWE 5 лет назад
haha, sure, no problem
@chenyangwang7232
@chenyangwang7232 4 года назад
@@BackToBackSWE Hahahahaha, you understand this?
@lyuki
@lyuki 2 года назад
Loved it! Thank you for putting this great content out!
@AgentKnopf
@AgentKnopf 4 года назад
Very cool, thank you 👍!! Will implement that today for practice 😁!
@BackToBackSWE
@BackToBackSWE 4 года назад
nice
@ShahafOfficial
@ShahafOfficial 2 года назад
you are great thank you!
@sakshiwahi2025
@sakshiwahi2025 3 года назад
Whoa. Where were you all this time 🙏
@ysheepy4907
@ysheepy4907 3 года назад
You are so helpful! Thank you for the excellent content!
@quocanhbach6696
@quocanhbach6696 3 года назад
another wonderful video, Thank you so much
@sye119
@sye119 3 года назад
God bless you Benyam! Great content as always!
@yanxichen4236
@yanxichen4236 5 лет назад
Great work as always. One question: how rarely does the operation need to happen for the amortized time to go down from the worst case to constant time?
@BackToBackSWE
@BackToBackSWE 5 лет назад
To be honest, I am not sure. I'm in Algorithms right now so I'm still learning. We would have to look at a concrete analysis (like I did with Bubble Sort and Selection Sort) and see the exact worst case operations that are done across many operations. We get the concrete analysis and then bound it based on what we find. For amortized analysis we wouldn't bound to a specific operation's worst case, we would bound against a large amount of operations' worst cases throughout the algorithm. Again, I'm not qualified to teach this in depth yet so I found a great resource here: www.cs.princeton.edu/~fiebrink/423/AmortizedAnalysisExplained_Fiebrink.pdf
@yanxichen4236
@yanxichen4236 5 лет назад
@@BackToBackSWE Thanks so much!
@BackToBackSWE
@BackToBackSWE 5 лет назад
@@yanxichen4236 sure
@volodymyrkot6277
@volodymyrkot6277 5 лет назад
You can look at this specific problem in this way - for every pop which takes O(N) time there will be N pops in O(1) time. This makes pop O(2N/(N+1))~O(2)~O(1) operation.
@joshuamarcano350
@joshuamarcano350 2 года назад
That is a great question
@ZaidFraij
@ZaidFraij 4 года назад
can you please make a video that goes deeper into Amortized analysis. Like also explaining the different methods to calculate the amortized time
@BackToBackSWE
@BackToBackSWE 4 года назад
yeah - but I'd have to read a paper on it, I only know the basics
@kellyxiao3060
@kellyxiao3060 5 лет назад
Love your video and appreciate your amazing work. and please keep doing this :).
@BackToBackSWE
@BackToBackSWE 5 лет назад
ok
@Suyashmehra0
@Suyashmehra0 5 лет назад
Brilliant !!
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@saucybaka3793
@saucybaka3793 5 лет назад
Love your work 😍
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@nikhilkumar9645
@nikhilkumar9645 5 лет назад
very nice explanation
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@anonymousvevo8697
@anonymousvevo8697 4 года назад
you explain much better , and you talk slower , i followed you better than last time
@BackToBackSWE
@BackToBackSWE 4 года назад
nice
@nandinirm2234
@nandinirm2234 2 года назад
Since u r soo good at explaining please I want u to explain step by step python code for it ...with black theme and mobile user friendly font size
@dhruvrajpurohit7341
@dhruvrajpurohit7341 5 лет назад
Awesome!
@BackToBackSWE
@BackToBackSWE 5 лет назад
hey
@nelsonthekinger
@nelsonthekinger 3 года назад
14:09 we acknowledge you!
@BackToBackSWE
@BackToBackSWE 3 года назад
ye
@messiah2230
@messiah2230 5 лет назад
please make a video on egg dropping problem
@BackToBackSWE
@BackToBackSWE 5 лет назад
ok
@messiah2230
@messiah2230 5 лет назад
@@BackToBackSWE thanks
@npc2481
@npc2481 4 года назад
Great video and give goal🔥
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@vishnuvardhan623
@vishnuvardhan623 5 лет назад
Some people argue that space is constant time as we create only stack pointer and we are moving the nodes
@BackToBackSWE
@BackToBackSWE 5 лет назад
Not sure what you mean, but no matter what, our structure underneath will hold n items if n items are given to us. Right? Do you agree?
@vishnuvardhan623
@vishnuvardhan623 5 лет назад
But queue itself has n items .Is it counted as O(n)??They are counting extra space excluded of n items
@BackToBackSWE
@BackToBackSWE 5 лет назад
@@vishnuvardhan623 If you don't include the items in the actual queue (which is really 2 stacks underneath) then yeah, space will be O(1) since we aren't creating any auxiliary space past the actual items.
@shivrajnag12
@shivrajnag12 5 лет назад
Fuck man.. u are awesome...Probably the best explaination i ever had listened on algorithm....
@BackToBackSWE
@BackToBackSWE 5 лет назад
wassup
@shivrajnag12
@shivrajnag12 5 лет назад
@@BackToBackSWE Hey Ben, I just want to know do these tech companies owner really know ds and algos... Do guys like Jack Dorsey really knew ds and algos prior to creating twitter...
@BackToBackSWE
@BackToBackSWE 5 лет назад
@@shivrajnag12 No
@davidacosta6383
@davidacosta6383 2 года назад
What's the music at the end credits? I really liked it. Shazam didn't pick it up either.🤔
@batman_1st
@batman_1st 5 лет назад
I'm digging that banana republic shirt
@BackToBackSWE
@BackToBackSWE 5 лет назад
I'm digging your vibe
@Firstusee256
@Firstusee256 5 лет назад
Great...
@BackToBackSWE
@BackToBackSWE 5 лет назад
hola mi amigo
@domyi6953
@domyi6953 5 лет назад
you are legendary
@BackToBackSWE
@BackToBackSWE 5 лет назад
the legend isn't complete. give me a few more years.
@domyi6953
@domyi6953 5 лет назад
@@BackToBackSWE you are on the right track! you got this man
@grunze
@grunze 4 года назад
This is a neat approach. Also could we not just check the length of the stack and just either pop from the nth -1 on dequeue and add to the 0th item on enqueue?
@BackToBackSWE
@BackToBackSWE 4 года назад
I would like to answer this but I'm not fully sure what you mean
@grunze
@grunze 4 года назад
@@BackToBackSWE ** if we override the Stack implementation by extending the Stack..** public synchronized E pop() { int len = size(); if (len == 0) throw new EmptyStackException(); removeElementAt(0); return elementAt(0); } Small test program: public static void main(String[] args) { // TODO Auto-generated method stub Stack mystack = new MyStack(); mystack.add(1); //First In mystack.add(5); mystack.add(3); mystack.add(7); //Last in System.out.println(mystack); mystack.pop(); // Removes 1 System.out.println(mystack); mystack.push(7); System.out.println(mystack); mystack.pop(); System.out.println(mystack); }
@grunze
@grunze 4 года назад
Sorry when I replied earlier I wasn't being clear on overriding the Stack. My apologies.
@rajarshi25.7
@rajarshi25.7 2 года назад
Should have included the peek Operation too
@nomidaepapi
@nomidaepapi 4 года назад
when we add items to the list, are you using the insert method or append?
@BackToBackSWE
@BackToBackSWE 4 года назад
either?
@nialyavuzturk379
@nialyavuzturk379 3 года назад
This video explains everything very well but I just have 1 questions, since the enqueue and dequeue methods take a fair ammount of code, should I put them into functions so I dont have to write it out each time?
@wl7497
@wl7497 5 лет назад
The expected output for empty queue is 1 on Leetcode...
@BackToBackSWE
@BackToBackSWE 5 лет назад
The code sample passes all test cases: github.com/bephrem1/backtobackswe/blob/master/Stacks%20%26%20Queues/queueWith2Stacks.java Where did I misspeak?
@wl7497
@wl7497 5 лет назад
Your code is correct, sry
@BackToBackSWE
@BackToBackSWE 5 лет назад
@@wl7497 haha it is ok, I'm just making sure
@siobhanahbois
@siobhanahbois 4 года назад
Spoiler alert: 7:32 second stack
@BackToBackSWE
@BackToBackSWE 4 года назад
shocker
@EllisiumVP
@EllisiumVP 5 лет назад
stack is also an abstract concept though :)
@BackToBackSWE
@BackToBackSWE 5 лет назад
yeah, where did I misspeak?
@noahmatisoff9518
@noahmatisoff9518 5 лет назад
It's am-or-tized, not amor-tized.
@BackToBackSWE
@BackToBackSWE 5 лет назад
ok
@PeyiOyelo
@PeyiOyelo 4 года назад
1 queue-hater disliked this video
@BackToBackSWE
@BackToBackSWE 4 года назад
thas' tru
@user-wo5yf8pt5e
@user-wo5yf8pt5e 5 лет назад
hhhhhhh u are so cute
@BackToBackSWE
@BackToBackSWE 5 лет назад
wut
@user-oo2gz9ln8v
@user-oo2gz9ln8v 4 года назад
man this sucks (not the video!) i’ve been programming for a while and i understand this intuitively but i don’t understand any of the terminology like “linear in runtime” does that just mean it happens step by step i have no idea
@BackToBackSWE
@BackToBackSWE 4 года назад
Watch my asymptotic complexity video, it'll click. It is an asymptotic statement. When n is huge, what does the graph of operations look like? A line? A curve? But in a more thorough sense it is all about functions.
@user-oo2gz9ln8v
@user-oo2gz9ln8v 4 года назад
Back To Back SWE jeez dude this is a blessing you seem to care a lot and i appreciate that. anyway i’ll check that out wanna say your teaching style of the most “clickable” i’ve learned from on youtube
@tanujatirkey3771
@tanujatirkey3771 3 года назад
You are awesome 👍❣️
Далее
Men Vs Women Survive The Wilderness For $500,000
31:48
Прохожу маску ЭМОЦИИ🙀 #юмор
00:59
Data Structures: Queue With Two Stacks
7:12
Просмотров 99 тыс.
Men Vs Women Survive The Wilderness For $500,000
31:48