Faaez Razeen

Implement Queue using Stacks

  • 2 min read
  • Queue
  • Stacks
  • LC-Easy

3 years ago

Solution 1 (Less Efficient)

TimeSpaceExplanation
O(n)O(2n)O(n) for push since we have to move all elements to another stack before pushing
class MyQueue: def __init__(self): self.s1 = [] self.s2 = [] def push(self, x: int) -> None: while self.s1: self.s2.append(self.s1.pop()) self.s1.append(x) while self.s2: self.s1.append(self.s2.pop()) def pop(self) -> int: return self.s1.pop() def peek(self) -> int: return self.s1[-1] def empty(self) -> bool: return len(self.s1) == 0

Solution 1 More Efficient)

TimeSpaceExplanation
O(1)O(2n)O(1) is amortized time complexity, not worst case
class MyQueue: def __init__(self): self.input = [] self.output = [] def push(self, x: int) -> None: self.input.append(x) def transfer(self) -> None: if not self.output: while self.input: self.output.append(self.input.pop()) def pop(self) -> int: self.transfer() return self.output.pop() def peek(self) -> int: self.transfer() return self.output[-1] def empty(self) -> bool: return not self.output and not self.input