LeetCode 1670. Design Front Middle Back Queue Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1670. Design Front Middle Back Queue

Description

Design a queue that supports push and pop operations in the front, middle, and back.

Implement the FrontMiddleBack class:

  • FrontMiddleBack() Initializes the queue.
  • void pushFront(int val) Adds val to the front of the queue.
  • void pushMiddle(int val) Adds val to the middle of the queue.
  • void pushBack(int val) Adds val to the back of the queue.
  • int popFront() Removes the front element of the queue and returns it. If the queue is empty, return -1.
  • int popMiddle() Removes the middle element of the queue and returns it. If the queue is empty, return -1.
  • int popBack() Removes the back element of the queue and returns it. If the queue is empty, return -1.

Notice that when there are two middle position choices, the operation is performed on the frontmost middle position choice. For example:

  • Pushing 6 into the middle of [1, 2, 3, 4, 5] results in [1, 2, 6, 3, 4, 5].
  • Popping the middle from [1, 2, 3, 4, 5, 6] returns 3 and results in [1, 2, 4, 5, 6].

 

Example 1:

Input:
["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", "popFront", "popMiddle", "popMiddle", "popBack", "popFront"]
[[], [1], [2], [3], [4], [], [], [], [], []]
Output:
[null, null, null, null, null, 1, 3, 4, 2, -1]

Explanation:
FrontMiddleBackQueue q = new FrontMiddleBackQueue();
q.pushFront(1);   // [1]
q.pushBack(2);    // [1, 2]
q.pushMiddle(3);  // [1, 3, 2]
q.pushMiddle(4);  // [1, 4, 3, 2]
q.popFront();     // return 1 -> [4, 3, 2]
q.popMiddle();    // return 3 -> [4, 2]
q.popMiddle();    // return 4 -> [2]
q.popBack();      // return 2 -> []
q.popFront();     // return -1 -> [] (The queue is empty)

 

Constraints:

  • 1 <= val <= 109
  • At most 1000 calls will be made to pushFrontpushMiddlepushBack, popFront, popMiddle, and popBack.

Solutions

Solution 1: Two Deques

We use two deques, where q1 stores the first half, and q2 stores the second half. The rebalance function is used to maintain the balance between the two queues, i.e., keeping the length of q2 greater than or equal to the length of q1, and the difference in length does not exceed 1.

In the pushFront, pushMiddle, and pushBack functions, we only need to add elements to q1 or q2, and call the rebalance function.

For the popFront function, we need to check whether q1 and q2 are empty. If both are empty, return -1. Otherwise, we need to check whether q1 is empty. If not, pop the front element of q1, otherwise pop the front element of q2, and call the rebalance function.

For the popMiddle function, we need to check whether q1 and q2 are empty. If both are empty, return -1. Otherwise, we need to check whether the lengths of q1 and q2 are equal. If they are equal, pop the last element of q1, otherwise pop the front element of q2, and call the rebalance function.

For the popBack function, we only need to check whether q2 is empty. If it is empty, return -1. Otherwise, pop the last element of q2, and call the rebalance function.

The time complexity of the above operations is O(1), and the space complexity is O(n), where n is the number of elements in the queue.

PythonJavaC++GoTypeScriptJavaScript
class FrontMiddleBackQueue: def __init__(self): self.q1 = deque() self.q2 = deque() def pushFront(self, val: int) -> None: self.q1.appendleft(val) self.rebalance() def pushMiddle(self, val: int) -> None: self.q1.append(val) self.rebalance() def pushBack(self, val: int) -> None: self.q2.append(val) self.rebalance() def popFront(self) -> int: if not self.q1 and not self.q2: return -1 if self.q1: val = self.q1.popleft() else: val = self.q2.popleft() self.rebalance() return val def popMiddle(self) -> int: if not self.q1 and not self.q2: return -1 if len(self.q1) == len(self.q2): val = self.q1.pop() else: val = self.q2.popleft() self.rebalance() return val def popBack(self) -> int: if not self.q2: return -1 val = self.q2.pop() self.rebalance() return val def rebalance(self): if len(self.q1) > len(self.q2): self.q2.appendleft(self.q1.pop()) if len(self.q2) > len(self.q1) + 1: self.q1.append(self.q2.popleft()) # Your FrontMiddleBackQueue object will be instantiated and called as such: # obj = FrontMiddleBackQueue() # obj.pushFront(val) # obj.pushMiddle(val) # obj.pushBack(val) # param_4 = obj.popFront() # param_5 = obj.popMiddle() # param_6 = obj.popBack()(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !