Implement Queue using Stacks - Leetcode 232 - Python

preview_player
Показать описание


0:00 - Read the problem
0:36 - Drawing Explanation
12:11 - Coding Explanation

leetcode 232

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

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.

NeetCodeIO
Автор

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!

servantofthelord
Автор

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.

garsidrag
Автор

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

Amy-
Автор

For peek, you don't need to perform the copy operation all over again. Just check if s2 has items, and if so return from position -1; else if s1 has items, return from position 0:
if s2:
return s2[-1]
elif s1:
return s1[0]

adventurer
Автор

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

garth
Автор

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

utkarshchaturvedi
Автор

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

SoRandominfo
Автор

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

Zefnx
Автор

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

mikerico
Автор

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

sourabpramanik
Автор

I was asked this question in my OOD round at Amazon. Messed it up :/

scoobyy
Автор

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

shaanvijaishankar
Автор

is this actually correct? i think you need to move everything from the second stack to the first before pushing.

neVessential
Автор

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

🙂

brucem
Автор

This is definitely not an "easy" for me, the concept is pretty damn hard to come up with on the spot

Jason-lblu
Автор

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]

ooouuuccchhh
Автор

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

dumbfailurekms