1670. Design Front Middle Back Queue

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.

Rust Solution

use std::collections::VecDeque;

#[derive(Debug)]
struct FrontMiddleBackQueue {
    front: VecDeque<i32>,
    back: VecDeque<i32>,
}

impl FrontMiddleBackQueue {
    fn new() -> Self {
        let front = VecDeque::new();
        let back = VecDeque::new();
        FrontMiddleBackQueue { front, back }
    }

    fn push_front(&mut self, val: i32) {
        self.front.push_front(val);
        if self.front.len() == self.back.len() + 2 {
            self.back.push_front(self.front.pop_back().unwrap());
        }
    }

    fn push_middle(&mut self, val: i32) {
        if self.front.len() == self.back.len() + 1 {
            self.back.push_front(self.front.pop_back().unwrap());
        }
        self.front.push_back(val);
    }

    fn push_back(&mut self, val: i32) {
        self.back.push_back(val);
        if self.front.len() + 1 == self.back.len() {
            self.front.push_back(self.back.pop_front().unwrap());
        }
    }

    fn pop_front(&mut self) -> i32 {
        if let Some(val) = self.front.pop_front() {
            if self.front.len() + 1 == self.back.len() {
                self.front.push_back(self.back.pop_front().unwrap());
            }
            val
        } else {
            -1
        }
    }

    fn pop_middle(&mut self) -> i32 {
        if let Some(val) = self.front.pop_back() {
            if self.front.len() + 1 == self.back.len() {
                self.front.push_back(self.back.pop_front().unwrap());
            }
            val
        } else {
            -1
        }
    }

    fn pop_back(&mut self) -> i32 {
        if let Some(val) = self.back.pop_back() {
            if self.front.len() == self.back.len() + 2 {
                self.back.push_front(self.front.pop_back().unwrap());
            }
            val
        } else {
            if let Some(val) = self.front.pop_back() {
                val
            } else {
                -1
            }
        }
    }
}

#[test]
fn test() {
    let mut q = FrontMiddleBackQueue::new();
    q.push_front(1);
    q.push_back(2);
    q.push_middle(3);
    q.push_middle(4);
    assert_eq!(q.pop_front(), 1);
    assert_eq!(q.pop_middle(), 3);
    assert_eq!(q.pop_middle(), 4);
    assert_eq!(q.pop_back(), 2);
    assert_eq!(q.pop_front(), -1);
}

Having problems with this solution? Click here to submit an issue on github.