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

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024

ความคิดเห็น • 142

  • @BackToBackSWE
    @BackToBackSWE  5 ปีที่แล้ว +18

    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 ปีที่แล้ว +1

      Encapsulation is a means to achieve abstraction

  • @angelluisgarciaguzman5598
    @angelluisgarciaguzman5598 4 ปีที่แล้ว +28

    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.

  • @levifoster-grundy240
    @levifoster-grundy240 4 ปีที่แล้ว +6

    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.

  • @cristina-gabrielagavrila8390
    @cristina-gabrielagavrila8390 5 ปีที่แล้ว +29

    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!

  • @tushar8998
    @tushar8998 4 ปีที่แล้ว +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.

  • @longuyen10119
    @longuyen10119 4 ปีที่แล้ว +6

    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 ปีที่แล้ว +3

      I've only been doing this since December 2018 haha so I guess 1.5? and nice

  • @abanerjee3704
    @abanerjee3704 3 ปีที่แล้ว

    I like how they nailed the staged rewind part. Good content bro. Keep it up.

  • @shivamchauhan3562
    @shivamchauhan3562 5 ปีที่แล้ว +2

    best thing about your videos is you don't feel bored

  • @AlekVila
    @AlekVila 2 ปีที่แล้ว

    This is a brilliant explanation. So clear!! And now I see a slinky.

  • @arunkumar777
    @arunkumar777 3 ปีที่แล้ว +1

    you put lots of effort in your video's bro kudos to that...

  • @helenashatalova7828
    @helenashatalova7828 2 ปีที่แล้ว

    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!

  • @kraanas
    @kraanas 4 ปีที่แล้ว +1

    Wow, what a great explanation! It's very easy to understand and interesting to listen :)

  • @charan_75
    @charan_75 3 ปีที่แล้ว +1

    Simple and superior as always.

  • @jonathanharris2326
    @jonathanharris2326 4 ปีที่แล้ว +3

    thank you, you're really good at explaining concepts

  • @julien.ambrosio
    @julien.ambrosio 2 ปีที่แล้ว

    Your explanation is amazing! Thanks for this video!!!!

  • @MonisYousuf
    @MonisYousuf 4 ปีที่แล้ว +2

    Brilliant explanation!!

  • @logovskii
    @logovskii ปีที่แล้ว +1

    best explanation

    • @BackToBackSWE
      @BackToBackSWE  ปีที่แล้ว

      Thank you 🎉 Please enjoy a special coupon from us - backtobackswe.com/checkout?plan=lifetime-legacy&discount_code=SUB 🚀

  • @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.

  • @anikpait5556
    @anikpait5556 4 ปีที่แล้ว +1

    Amazing and Clear Explanations! Kudos!

    • @BackToBackSWE
      @BackToBackSWE  4 ปีที่แล้ว

      thanks for watching

    • @anikpait5556
      @anikpait5556 4 ปีที่แล้ว

      @@BackToBackSWE would love your videos on System Design if possible :)

  • @ricardopereira5397
    @ricardopereira5397 2 ปีที่แล้ว

    Great explanation! Thanks for sharing, subscribed and liked the video.

  • @b747
    @b747 5 ปีที่แล้ว +7

    thanks for the great channel ...you are my new hero! ✌️👍

  • @rakeshreddyabbireddy8876
    @rakeshreddyabbireddy8876 4 ปีที่แล้ว +3

    firstly I feared about dynamic programming but now it is a cakewalk after your watching videos and now queues and stacks also

  • @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 :)

  • @QVL75
    @QVL75 2 ปีที่แล้ว

    Awesome explanation. Thanks!

  • @yanxichen4236
    @yanxichen4236 5 ปีที่แล้ว +8

    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 ปีที่แล้ว +3

      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 ปีที่แล้ว +2

      @@BackToBackSWE Thanks so much!

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      @@yanxichen4236 sure

    • @volodymyrkot6277
      @volodymyrkot6277 5 ปีที่แล้ว +1

      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

  • @dinohunter7176
    @dinohunter7176 3 ปีที่แล้ว

    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)

  • @ythalorossy
    @ythalorossy 5 ปีที่แล้ว +2

    Amazing article. Thank you very much.

  • @yicai7
    @yicai7 3 ปีที่แล้ว

    WHAT A GENIUS!

  • @unique00imagination
    @unique00imagination 2 ปีที่แล้ว +1

    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

  • @lyuki
    @lyuki 2 ปีที่แล้ว

    Loved it! Thank you for putting this great content out!

  • @sye119
    @sye119 4 ปีที่แล้ว

    God bless you Benyam! Great content as always!

  • @democratcobra
    @democratcobra 3 ปีที่แล้ว

    Thanks for uploading this...This is very helpfull to me.

  • @Voidangular0100
    @Voidangular0100 4 ปีที่แล้ว

    Great explanation

  • @yosef-alaker
    @yosef-alaker ปีที่แล้ว

    كل الشكر التقدير لك من متابع عربي ❤❤

  • @quocanhbach6696
    @quocanhbach6696 3 ปีที่แล้ว

    another wonderful video, Thank you so much

  • @sankalparora9261
    @sankalparora9261 4 ปีที่แล้ว +1

    You make stuff damn simple. Thank you.

  • @rkjohn441
    @rkjohn441 2 ปีที่แล้ว

    how such a high quality video!🔥

  • @yodkwtf
    @yodkwtf 2 ปีที่แล้ว

    great explaination. Really appreciate it

  • @ysheepy4907
    @ysheepy4907 3 ปีที่แล้ว

    You are so helpful! Thank you for the excellent content!

  • @tanujatirkey3771
    @tanujatirkey3771 3 ปีที่แล้ว

    You are awesome 👍❣️

  • @sahaneakanayaka3394
    @sahaneakanayaka3394 3 ปีที่แล้ว

    Thanks Ben 😄😄

  • @kellyxiao3060
    @kellyxiao3060 5 ปีที่แล้ว +2

    Love your video and appreciate your amazing work. and please keep doing this :).

  • @davidacosta6383
    @davidacosta6383 3 ปีที่แล้ว

    What's the music at the end credits?
    I really liked it. Shazam didn't pick it up either.🤔

  • @ZaidFraij
    @ZaidFraij 4 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      yeah - but I'd have to read a paper on it, I only know the basics

  • @grunze
    @grunze 5 ปีที่แล้ว +1

    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  5 ปีที่แล้ว

      I would like to answer this but I'm not fully sure what you mean

    • @grunze
      @grunze 5 ปีที่แล้ว

      @@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 5 ปีที่แล้ว

      Sorry when I replied earlier I wasn't being clear on overriding the Stack. My apologies.

  • @AgentKnopf
    @AgentKnopf 4 ปีที่แล้ว +1

    Very cool, thank you 👍!! Will implement that today for practice 😁!

  • @renjieyu8700
    @renjieyu8700 5 ปีที่แล้ว +4

    哈哈,谢谢大兄弟清晰的解释😀 爱你

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      haha, sure, no problem

    • @chenyangwang7232
      @chenyangwang7232 4 ปีที่แล้ว

      @@BackToBackSWE Hahahahaha, you understand this?

  • @nikhilkumar9645
    @nikhilkumar9645 5 ปีที่แล้ว +1

    very nice explanation

  • @vrashankraom
    @vrashankraom 4 ปีที่แล้ว +2

    Awesome liked it!

  • @trishulsherigar1961
    @trishulsherigar1961 4 ปีที่แล้ว +1

    icredible explanation..

  • @nomidaepapi
    @nomidaepapi 5 ปีที่แล้ว +1

    when we add items to the list, are you using the insert method or append?

  • @freesoul2677
    @freesoul2677 2 ปีที่แล้ว

    Thank you so much!

  • @saucybaka3793
    @saucybaka3793 5 ปีที่แล้ว +1

    Love your work 😍

  • @sakshiwahi2025
    @sakshiwahi2025 3 ปีที่แล้ว

    Whoa. Where were you all this time 🙏

  • @ShahafOfficial
    @ShahafOfficial 3 ปีที่แล้ว

    you are great thank you!

  • @npc2481
    @npc2481 4 ปีที่แล้ว +1

    Great video and give goal🔥

  • @Theberner0
    @Theberner0 5 ปีที่แล้ว +1

    Brilliant !!

  • @anonymousvevo8697
    @anonymousvevo8697 4 ปีที่แล้ว +1

    you explain much better , and you talk slower , i followed you better than last time

  • @messiah2230
    @messiah2230 5 ปีที่แล้ว +5

    please make a video on egg dropping problem

  • @vishnuvardhan623
    @vishnuvardhan623 5 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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.

  • @nialyavuzturk379
    @nialyavuzturk379 3 ปีที่แล้ว +1

    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?

  • @dhruvrajpurohit7341
    @dhruvrajpurohit7341 5 ปีที่แล้ว +1

    Awesome!

  • @domyi6953
    @domyi6953 5 ปีที่แล้ว +1

    you are legendary

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +2

      the legend isn't complete. give me a few more years.

    • @domyi6953
      @domyi6953 5 ปีที่แล้ว +1

      @@BackToBackSWE you are on the right track! you got this man

  • @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

  • @nelsonthekinger
    @nelsonthekinger 4 ปีที่แล้ว +1

    14:09 we acknowledge you!

  • @Firstusee256
    @Firstusee256 5 ปีที่แล้ว +1

    Great...

  • @siobhanahbois
    @siobhanahbois 4 ปีที่แล้ว +2

    Spoiler alert:
    7:32 second stack

  • @batman_1st
    @batman_1st 5 ปีที่แล้ว +1

    I'm digging that banana republic shirt

  • @wl7497
    @wl7497 5 ปีที่แล้ว +2

    The expected output for empty queue is 1 on Leetcode...

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      Your code is correct, sry

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      @@wl7497 haha it is ok, I'm just making sure

  • @shivrajnag12
    @shivrajnag12 5 ปีที่แล้ว +1

    Fuck man.. u are awesome...Probably the best explaination i ever had listened on algorithm....

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +1

      wassup

    • @shivrajnag12
      @shivrajnag12 5 ปีที่แล้ว +1

      @@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

  • @rajarshi25.7
    @rajarshi25.7 2 ปีที่แล้ว

    Should have included the peek Operation too

  • @EllisiumVP
    @EllisiumVP 5 ปีที่แล้ว +2

    stack is also an abstract concept though :)

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +3

      yeah, where did I misspeak?

  • @PeyiOyelo
    @PeyiOyelo 5 ปีที่แล้ว +3

    1 queue-hater disliked this video

  • @noahmatisoff9518
    @noahmatisoff9518 5 ปีที่แล้ว +1

    It's am-or-tized, not amor-tized.

  • @user-oo2gz9ln8v
    @user-oo2gz9ln8v 5 ปีที่แล้ว +1

    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  5 ปีที่แล้ว

      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 5 ปีที่แล้ว +1

      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

  • @曾成成-d7b
    @曾成成-d7b 5 ปีที่แล้ว +2

    hhhhhhh u are so cute

  • @tanujatirkey3771
    @tanujatirkey3771 3 ปีที่แล้ว

    You are awesome 👍❣️