bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1670. Design Front Middle Back Queue

1670. Design Front Middle Back Queue

Leetcode Solutions

Using Two Deques

  1. Initialize two deques, left and right.
  2. For pushFront, add the element to the front of left.
  3. For pushBack, add the element to the back of right.
  4. For pushMiddle, if left has more elements, move the last element of left to the front of right, then add the new element to the back of left. Otherwise, add the new element to the front of right.
  5. For popFront, if left is empty, return -1. Otherwise, pop from the front of left and balance the deques if necessary.
  6. For popMiddle, if the total size is even, pop from the back of left; if odd, pop from the front of right. Balance the deques if necessary.
  7. For popBack, if right is empty, pop from the back of left. Otherwise, pop from the back of right.
UML Thumbnail

Using a Single List

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...