Implement Queue using Stacks - Leetcode 232 - Python

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
    🐦 Twitter: / neetcode1
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    Problem Link: leetcode.com/problems/impleme...
    0:00 - Read the problem
    0:36 - Drawing Explanation
    12:11 - Coding Explanation
    leetcode 232
    #neetcode #leetcode #python

Komentáře • 35

  • @NeetCodeIO
    @NeetCodeIO  Před 5 měsíci +38

    Even though this is an easy, I spent some extra time to really explain why the solution works.
    I think it's tricky enough to warrant that, but let me know if it felt too long though.

    • @akshayiithyd
      @akshayiithyd Před 5 měsíci +1

      Really liked the O(1) explanation, thanx.
      Can you make the video explaining what does the term amortized exactly mean, can we assume it as same as avg time?

    • @sanjaycse9608
      @sanjaycse9608 Před 5 měsíci

      ​@@akshayiithydno that was constant time operation

    • @va11bhav_rana
      @va11bhav_rana Před 5 měsíci

      honestly, i only do Leetcode just when you do, so please don't stop

    • @KhyatiSatija
      @KhyatiSatija Před 5 měsíci

      no its perfect.

    • @singletmat5172
      @singletmat5172 Před 5 měsíci

      I appreciate the longer explanations, it’s very thorough and it clears a lot of confusion for me. And I also appreciate you mentioning that you wouldn’t be able to solve it if you did it for the first time. 😅

  • @servantofthelord8147
    @servantofthelord8147 Před měsícem +2

    I used to watch your videos on 1.5x speed, yet I always had to rewind because I kept missing the logic. Once I humbled myself and decided to slow it down to 1x speed - I have a much more thorough understanding of your content. THANK YOU!

  • @utkarshchaturvedi2405
    @utkarshchaturvedi2405 Před 5 měsíci

    Found out today that you solve daily problems , will be following these videos from today♥

  • @garsidrag
    @garsidrag Před 5 měsíci +19

    i think you should really consider changing it up and once in a while instead of saying its pretty efficient, you should say its hella efficient. would be very satisfying to hear.

  • @Amy-601
    @Amy-601 Před 5 měsíci +2

    Literally just solved it today after 4 years. I think it was daily challenge or something. I remembered to push onto in-stack and pop from outstack, checking if it’s empty and doing an outstack.push( in stack.pop()). Lol 😂. But I liked your “ amortized” explanation, didn’t think of “ amortized”. - Amy

  • @davidkent5749
    @davidkent5749 Před 5 měsíci +2

    Plz add this question to neetcode all on ur website it will be really helpful.

  • @krateskim4169
    @krateskim4169 Před 5 měsíci +1

    Great video

  • @chien-yuyeh9386
    @chien-yuyeh9386 Před 5 měsíci

    Nice🎉🎉

  • @garth2356
    @garth2356 Před 5 měsíci +4

    Great solution and explanation but this shouldn't be an *easy* question :(

  • @Zefn1x
    @Zefn1x Před 5 měsíci +5

    Man fuked up google’s Interview. Will be grinding your sheet and doing LT contests. Thanks for your explanation! ❤

    • @jeffraj9937
      @jeffraj9937 Před 5 měsíci +4

      Same here messed up with a simple question. Started grinding Daily..

  • @tizekodnoon8305
    @tizekodnoon8305 Před 5 měsíci

    Neato! Brain fried! 🧠🤯

  • @pastori2672
    @pastori2672 Před 5 měsíci

    nice

  • @michaelyao9389
    @michaelyao9389 Před měsícem

    How to come up with this idea...

  • @sourabpramanik996
    @sourabpramanik996 Před 5 měsíci +1

    Do you think it is possible to do it using one stack only (if there is a follow up question)? I think it is possible but it won't be memory efficient I guess

    • @arturpelcharskyi1281
      @arturpelcharskyi1281 Před 5 měsíci

      You can. I came up with 2 ways:
      1. Create a pointer that will refer to the element that should be returned when the pop() or peek() function is called. If we call pop(), we move this pointer forward by 1 element. In push() we add elements to the end of the list, and in the function empty() we return: pointer == len(stack).
      2. If you write in Python, you can set the stack as a list, and when you call pop(), return the result of pop(0). Then 0 element will be removed from the list and its value will be returned.
      If we compare these two methods, the first requires more memory, but all functions are executed in O(1), the second method uses less memory, but the pop() method can take O(n) time depending on the specifics of the implementation remove 0 item from list in different languages.

    • @leeroymlg4692
      @leeroymlg4692 Před 5 měsíci

      @@arturpelcharskyi1281 that's breaking the rules, because in a stack, you can only look at the top of the stack (the last element)

    • @sourabpramanik996
      @sourabpramanik996 Před 5 měsíci

      @@arturpelcharskyi1281great solve. I solved using the first approach

  • @shaanvijaishankar5314
    @shaanvijaishankar5314 Před 2 měsíci

    Why is this video not listed in 'Neetcode All'?

  • @mikerico6175
    @mikerico6175 Před 5 měsíci +1

    @NeetcodeIO , you should create a private method to avoid duplucate code lines 11 and 17

    • @NeetCodeIO
      @NeetCodeIO  Před 5 měsíci +9

      I prefer WET (write everything twice) over DRY (don't repeat yourself) 😝

    • @michael._.
      @michael._. Před 5 měsíci +3

      TIL what WET and DRY meant... learning something new every time I watch neetcode's videos

    • @dera_ng
      @dera_ng Před 5 měsíci

      ​@@NeetCodeIO 🫠

  • @brucem8448
    @brucem8448 Před 5 měsíci

    Queues are FIFO. Stacks are LIFO.
    Queue, original order: user1, user2, user3
    Queue, popping: user3, user2, user1...
    Queue popping reverses order.
    This is why 2 (!) queues are necessary - reversing reversal preserves the original order
    🙂

  • @ooouuuccchhh
    @ooouuuccchhh Před 5 měsíci

    in the peek method, you can just return the first element of the stack1 rather than again popping and appending the elements.
    it can go like
    if not self.s2:
    return self.s1[0]

    • @mzafarr
      @mzafarr Před 5 měsíci +1

      But that violates property of stack, because in stack you can only access the last element.

    • @ooouuuccchhh
      @ooouuuccchhh Před 5 měsíci +1

      @@mzafarr got it!

    • @Baseballchampion
      @Baseballchampion Před 5 měsíci

      See 11:20

  • @dumbfailurekms
    @dumbfailurekms Před 5 měsíci

    When I first started watching your videos I would probably look at this problem and take some time to do the O(n) solution. After about a year of practicing, there was literally not a microsecond that I even considered to solve it in O(n) because I instantly realized that would be trivial and the only real problem is doing this in amortized O(1). point is you helped me a lot to grow