Leetcode 232. Implement Queue using Stacks - Python

  Рет қаралды 12,814

HelmyCodeCamp

HelmyCodeCamp

Күн бұрын

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
Implement the MyQueue class:
void push(int x) Pushes element x to the back of the queue.
int pop() Removes the element from the front of the queue and returns it.
int peek() Returns the element at the front of the queue.
boolean empty() Returns true if the queue is empty, false otherwise.
Notes:
You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.

Пікірлер: 16
@ИльяСмирнов-ж8я
@ИльяСмирнов-ж8я 15 күн бұрын
This feeling then you are a completely noob in Python, went to KZbin because you code didn't work... and see this video and think about pop(0) instead of the second stack.
@khalilsleimi7832
@khalilsleimi7832 Жыл бұрын
An alternative solution might be to have one stack hold input values, and the other stack holds the output values. You just add to the input stack when pushing. And when you need to peek or pop, you just check if the output stack is nonempty and return the poped/peeked value from that stack, otherwise, you flip the values from the input to the output and then pop/peek. Of course, it'll depend on where you want most of the computing to be spent, when writing data or when reading data
@jaskibrother
@jaskibrother 2 жыл бұрын
This is a great solution and explanation!!
@santhakumar20
@santhakumar20 Жыл бұрын
such a awesome explanation ...
@Guy9221
@Guy9221 Жыл бұрын
Time complexity analysis?
@nandinirm2234
@nandinirm2234 2 жыл бұрын
Good explanation Font size should have been mobile user friendly
@nagendrabommireddi8437
@nagendrabommireddi8437 2 жыл бұрын
thank you
@mitramir5182
@mitramir5182 Жыл бұрын
Thank you so much, I was really lost since I've just started learning about data structures! but your explanation was amazing!
@tomleeyrc40
@tomleeyrc40 Жыл бұрын
This reminds me of the towers of hanoi solution, thank you for this explanation
@pythonicd1239
@pythonicd1239 Жыл бұрын
I got memory limit exceeded for the same code but thanks for the video
@kiitanayandosu-ug7pe
@kiitanayandosu-ug7pe 11 ай бұрын
Amazing explanation
@maged_helmy
@maged_helmy 10 ай бұрын
Glad it was helpful!
@nikitasinha8181
@nikitasinha8181 Жыл бұрын
Thank you so much
@maged_helmy
@maged_helmy 10 ай бұрын
You're most welcome
@lucynowacki3327
@lucynowacki3327 Жыл бұрын
.push() is O(n) , .pop() is O(1)
@sriharsha2027
@sriharsha2027 Жыл бұрын
Thanks a lot bro your explanation is very good
Implement Queue using Stacks - Leetcode 232 - Python
15:23
NeetCodeIO
Рет қаралды 26 М.
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 38 МЛН
The day of the sea 😂 #shorts by Leisi Crazy
00:22
Leisi Crazy
Рет қаралды 2,3 МЛН
Every parent is like this ❤️💚💚💜💙
00:10
Like Asiya
Рет қаралды 17 МЛН
Being Competent With Coding Is More Fun
11:13
TheVimeagen
Рет қаралды 84 М.
LEETCODE 232 (JAVASCRIPT) | IMPLEMENT QUEUE USING STACKS
11:19
Stacks and Queues (Python) - Data Structures and Algorithms
14:10
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 236 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 38 М.
Is Computer Science still worth it?
20:08
NeetCodeIO
Рет қаралды 349 М.
Compiled Python is FAST
12:57
Doug Mercer
Рет қаралды 111 М.
I solved 541 Leetcode problems. But you need only 150.
7:42
Sahil & Sarra
Рет қаралды 2,4 МЛН
Queue Implementation using Stack | O(1) Push and Pop Operations
11:20
take U forward
Рет қаралды 167 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 90 М.
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 38 МЛН