From 5de48dab2c3cca6d926f85d0212dc580892bcdcd Mon Sep 17 00:00:00 2001 From: hitesh Date: Fri, 10 Apr 2026 17:06:28 -0700 Subject: [PATCH] Implement Queue with Stacks --- ImplementQueueWithStacks.py | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ImplementQueueWithStacks.py diff --git a/ImplementQueueWithStacks.py b/ImplementQueueWithStacks.py new file mode 100644 index 00000000..042fea66 --- /dev/null +++ b/ImplementQueueWithStacks.py @@ -0,0 +1,45 @@ +# TimeComplexity +## Push --> O(1), Pop --> O(N), Amortized O(1), Peek --> O(1), Empty --> O(1) +# Space Complexity +## O(N) + +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach +## Utilizing 2 stacks, one to push all the inputs coming in and the other to pop out the 1st element + +class MyQueue: + + def __init__(self): + self.st1 = [] + self.st2 = [] + + def push(self, x: int) -> None: + self.st1.append(x) + + def pop(self) -> int: + if len(self.st2)==0: + for i in range(len(self.st1)): + self.st2.append(self.st1.pop()) + return self.st2.pop() + + def peek(self) -> int: + if len(self.st2)==0: + for i in range(len(self.st1)): + self.st2.append(self.st1.pop()) + return self.st2[-1] + + def empty(self) -> bool: + if len(self.st1)==0 and len(self.st2)==0: + return True + return False + + + +# Your MyQueue object will be instantiated and called as such: +# obj = MyQueue() +# obj.push(x) +# param_2 = obj.pop() +# param_3 = obj.peek() +# param_4 = obj.empty() \ No newline at end of file