Implement Queue using Stacks - Leetcode 232 - Python

  Рет қаралды 30,448

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 39
@NeetCodeIO
@NeetCodeIO 10 ай бұрын
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 10 ай бұрын
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 10 ай бұрын
​@@akshayiithydno that was constant time operation
@va11bhav_rana
@va11bhav_rana 10 ай бұрын
honestly, i only do Leetcode just when you do, so please don't stop
@KhyatiSatija
@KhyatiSatija 10 ай бұрын
no its perfect.
@singletmat5172
@singletmat5172 10 ай бұрын
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 5 ай бұрын
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!
@mohdyasir4946
@mohdyasir4946 3 ай бұрын
ohh no i am watching in 2x.
@servantofthelord8147
@servantofthelord8147 3 ай бұрын
@@mohdyasir4946 Lol don't worry, there's definitely a time and place to watch the videos quicker. I learned this after I made my original comment
@Amy-601
@Amy-601 10 ай бұрын
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
@garsidrag
@garsidrag 10 ай бұрын
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.
@garth2356
@garth2356 10 ай бұрын
Great solution and explanation but this shouldn't be an *easy* question :(
@SoRandominfo
@SoRandominfo 10 ай бұрын
Plz add this question to neetcode all on ur website it will be really helpful.
@utkarshchaturvedi2405
@utkarshchaturvedi2405 10 ай бұрын
Found out today that you solve daily problems , will be following these videos from today♥
@krateskim4169
@krateskim4169 10 ай бұрын
Great video
@scoobyy3112
@scoobyy3112 6 күн бұрын
I was asked this question in my OOD round at Amazon. Messed it up :/
@mikerico6175
@mikerico6175 10 ай бұрын
@NeetcodeIO , you should create a private method to avoid duplucate code lines 11 and 17
@NeetCodeIO
@NeetCodeIO 10 ай бұрын
I prefer WET (write everything twice) over DRY (don't repeat yourself) 😝
@michael._.
@michael._. 10 ай бұрын
TIL what WET and DRY meant... learning something new every time I watch neetcode's videos
@dera_ng
@dera_ng 10 ай бұрын
​@@NeetCodeIO 🫠
@sourabpramanik996
@sourabpramanik996 10 ай бұрын
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 10 ай бұрын
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 10 ай бұрын
@@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 10 ай бұрын
@@arturpelcharskyi1281great solve. I solved using the first approach
@Zefn1x
@Zefn1x 10 ай бұрын
Man fuked up google’s Interview. Will be grinding your sheet and doing LT contests. Thanks for your explanation! ❤
@jeffraj9937
@jeffraj9937 10 ай бұрын
Same here messed up with a simple question. Started grinding Daily..
@neVessential
@neVessential Ай бұрын
is this actually correct? i think you need to move everything from the second stack to the first before pushing.
@chien-yuyeh9386
@chien-yuyeh9386 10 ай бұрын
Nice🎉🎉
@brucem8448
@brucem8448 10 ай бұрын
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 🙂
@tizekodnoon8305
@tizekodnoon8305 10 ай бұрын
Neato! Brain fried! 🧠🤯
@shaanvijaishankar5314
@shaanvijaishankar5314 6 ай бұрын
Why is this video not listed in 'Neetcode All'?
@pastori2672
@pastori2672 10 ай бұрын
nice
@michaelyao9389
@michaelyao9389 5 ай бұрын
How to come up with this idea...
@ooouuuccchhh
@ooouuuccchhh 10 ай бұрын
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 10 ай бұрын
But that violates property of stack, because in stack you can only access the last element.
@ooouuuccchhh
@ooouuuccchhh 10 ай бұрын
@@mzafarr got it!
@Baseballchampion
@Baseballchampion 10 ай бұрын
See 11:20
@dumbfailurekms
@dumbfailurekms 10 ай бұрын
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
First Unique Character in a String - Leetcode 387 - Python
4:35
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН
What type of pedestrian are you?😄 #tiktok #elsarca
00:28
Elsa Arca
Рет қаралды 35 МЛН
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 7 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
Stacks & Queues - DSA Course in Python Lecture 5
14:58
Greg Hogg
Рет қаралды 11 М.
Implement Trie (Prefix Tree) - Leetcode 208
18:56
NeetCode
Рет қаралды 213 М.
Queue Implementation using Stack | O(1) Push and Pop Operations
11:20
take U forward
Рет қаралды 172 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 63 М.
Top 6 Coding Interview Concepts (Data Structures & Algorithms)
10:51
First Bad Version - Leetcode 278 - Binary Search (Python)
8:12
Greg Hogg
Рет қаралды 4,6 М.
Subarray Product Less Than K - Leetcode 713 - Python
10:26
NeetCodeIO
Рет қаралды 17 М.
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН