Great explanation. But i'm wondering why you don't rebuild the pushStack back up after you have popped element#1? A few examples also do not rebuild the pushStack back up.
@samuelanumudu9080
2 жыл бұрын
Thank you, Andy. Your explanation helped me understand this better.
@rotimidokun
2 жыл бұрын
Wow, you did a wonderful job. Very clear explanation.
@NavyaVedachala
2 жыл бұрын
Why did we have to move elements into the pop stack for the peek ()? If pop stack is empty, could we not to pushstack[0]?
@ayalaanin
11 ай бұрын
Just adhering to the Algo, we can access any element in the array simply by index, as per algo it should be top, for that we move to pop stack and take the top.
@LSUHEBERT
2 жыл бұрын
class MyQueue{ constructor(){ this.pushStack = [] this.popStack = [] } push(val){ this.pushStack.push(val) } pop(){ while(this.pushStack.length){ this.popStack.push(this.pushStack.pop()) } let answer = this.popStack.pop() //rebuild push stack while(this.popStack.length){ this.pushStack.push(this.popStack.pop()) } return answer } peek(){ return this.pushStack[0] } empty(){ return this.pushStack.length === 0 } } was my solution for increased readability / simplicity
@Sam-nc6xt
2 жыл бұрын
Great explanation! Please keep making such videos :)
@meandmauve
2 жыл бұрын
Wonderful explanation. Thank you :)
@flekone93
3 жыл бұрын
Thanks, nice explanation!
@dequentinmiller9086
Жыл бұрын
just one question! Why did you put "-1" when returning the peek?
Пікірлер: 11