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

Поділитися
Вставка
  • Опубліковано 16 гру 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.

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

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

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

  • @AlekVila
    @AlekVila 2 роки тому

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

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

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

  • @julien.ambrosio
    @julien.ambrosio 2 роки тому

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

  • @MonisYousuf
    @MonisYousuf 4 роки тому +2

    Brilliant explanation!!

  • @arunkumar777
    @arunkumar777 3 роки тому +1

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

  • @jonathanharris2326
    @jonathanharris2326 4 роки тому +3

    thank you, you're really good at explaining concepts

  • @charan_75
    @charan_75 3 роки тому +1

    Simple and superior as always.

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

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

  • @ricardopereira5397
    @ricardopereira5397 2 роки тому

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

  • @kraanas
    @kraanas 4 роки тому +1

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

  • @QVL75
    @QVL75 2 роки тому

    Awesome explanation. Thanks!

  • @b747
    @b747 5 років тому +7

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

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

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

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

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

  • @ythalorossy
    @ythalorossy 5 років тому +2

    Amazing article. Thank you very much.

  • @yicai7
    @yicai7 3 роки тому

    WHAT A GENIUS!

  • @sankalparora9261
    @sankalparora9261 4 роки тому +1

    You make stuff damn simple. Thank you.

  • @lyuki
    @lyuki 2 роки тому

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

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

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

  • @quocanhbach6696
    @quocanhbach6696 3 роки тому

    another wonderful video, Thank you so much

  • @Voidangular0100
    @Voidangular0100 3 роки тому

    Great explanation

  • @sahaneakanayaka3394
    @sahaneakanayaka3394 3 роки тому

    Thanks Ben 😄😄

  • @democratcobra
    @democratcobra 3 роки тому

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

  • @sye119
    @sye119 4 роки тому

    God bless you Benyam! Great content as always!

  • @rkjohn441
    @rkjohn441 2 роки тому

    how such a high quality video!🔥

  • @AgentKnopf
    @AgentKnopf 4 роки тому +1

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

  • @ysheepy4907
    @ysheepy4907 3 роки тому

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

  • @kellyxiao3060
    @kellyxiao3060 5 років тому +2

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

  • @nikhilkumar9645
    @nikhilkumar9645 5 років тому +1

    very nice explanation

  • @tanujatirkey3771
    @tanujatirkey3771 3 роки тому

    You are awesome 👍❣️

  • @yodkwtf
    @yodkwtf 2 роки тому

    great explaination. Really appreciate it

  • @vrashankraom
    @vrashankraom 4 роки тому +2

    Awesome liked it!

  • @yosef-alaker
    @yosef-alaker Рік тому

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

  • @renjieyu8700
    @renjieyu8700 5 років тому +4

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

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

  • @saucybaka3793
    @saucybaka3793 5 років тому +1

    Love your work 😍

  • @trishulsherigar1961
    @trishulsherigar1961 4 роки тому +1

    icredible explanation..

  • @sakshiwahi2025
    @sakshiwahi2025 3 роки тому

    Whoa. Where were you all this time 🙏

  • @npc2481
    @npc2481 4 роки тому +1

    Great video and give goal🔥

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

  • @Theberner0
    @Theberner0 5 років тому +1

    Brilliant !!

  • @freesoul2677
    @freesoul2677 2 роки тому

    Thank you so much!

  • @nelsonthekinger
    @nelsonthekinger 4 роки тому +1

    14:09 we acknowledge you!

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

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

  • @ShahafOfficial
    @ShahafOfficial 3 роки тому

    you are great thank you!

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

  • @nomidaepapi
    @nomidaepapi 5 років тому +1

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

  • @dhruvrajpurohit7341
    @dhruvrajpurohit7341 5 років тому +1

    Awesome!

  • @siobhanahbois
    @siobhanahbois 4 роки тому +2

    Spoiler alert:
    7:32 second stack

  • @batman_1st
    @batman_1st 5 років тому +1

    I'm digging that banana republic shirt

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

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

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

  • @Firstusee256
    @Firstusee256 5 років тому +1

    Great...

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

  • @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 👍❣️