We use two stacks: s1 for pushing elements and s2 for popping/peeking. When s2 is empty, we transfer all elements from s1 to s2, which reverses their order and achieves FIFO behavior.
push(x): Append tos1.pop():- If
s2is empty, pop all elements froms1and push them tos2. - Pop from
s2.
- If
peek():- If
s2is empty, transfer froms1tos2. - Return the top of
s2.
- If
empty(): Return true if both stacks are empty.
- Time Complexity:
- Push: O(1)
- Pop/Peek: Amortized O(1). Each element is moved at most twice.
- Space Complexity: O(N).
class MyQueue:
def __init__(self):
self.s1 = []
self.s2 = []
def push(self, x: int) -> None:
self.s1.append(x)
def _transfer(self):
if not self.s2:
while self.s1:
self.s2.append(self.s1.pop())
def pop(self) -> int:
self._transfer()
return self.s2.pop()
def peek(self) -> int:
self._transfer()
return self.s2[-1]
def empty(self) -> bool:
return not self.s1 and not self.s2