“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Design the front, middle and rear queues

The general idea is: for a queue/array, add/remove a new element in the first position, add/remove an element in the last position, and add/remove an element in the middle position.

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

To complete the FrontMiddleBack class, use the following methods:

  • FrontMiddleBack() initializes the queue.
  • Void pushFront (int val)valAdded to the queueThe front of 。
  • Void pushMiddle (int val)valAdded to the queueRight in the middle 。
  • Void pushBack (int val)valAdded to the teamThe back 。
  • Int popFront() removes the uppermost element from the queue and returns the value, or -1 if the queue was empty before the deletion.
  • Int popMiddle() removes the middle element from the queue and returns the value, or -1 if the queue was empty before deletion.
  • Int popBack() removes the last element from the queue and returns the value, or -1 if the queue was empty before deletion.

Note that when there are two middle positions, choose the front position for operation. Such as:

  • Add 6 to the middle of [1, 2, 3, 4, 5], resulting in an array of [1, 2, 6, 3, 4, 5].
  • Pop the element from the middle of [1, 2, 3, 4, 5, 6], return 3, and the array becomes [1, 2, 4, 5, 6].

Code implementation:

/** @lc code= leetcode.cn id=1670 lang=javascript ** [1670] Design before, middle and after queue */ // @LC code=start /** * @description initialize queue */ var  FrontMiddleBackQueue = function() { this.leftArray = []; this.rightArray = []; }; / * * * @ the description will be added to the front of the queue val * @ param {number} val * @ return void * / FrontMiddleBackQueue. Attach the prototype. PushFront =  function(val) { this.leftArray.unshift(val); if (this.leftArray.length > this.rightArray.length) { this.rightArray.unshift(this.leftArray.pop()) } }; / * * * @ the right in the middle of the description will be val added to the queue * @ param {number} val * @ return void * / FrontMiddleBackQueue. Attach the prototype. PushMiddle = function(val) { if (this.leftArray.length == this.rightArray.length) { this.rightArray.unshift(val) } else { this.leftArray.push(val); }}; / * * * @ description add val to the team behind one of the most * @ param {number} val * @ return void * / FrontMiddleBackQueue. Attach the prototype. The pushBack = function(val) { this.rightArray.push(val) if (this.rightArray.length == this.leftArray.length + 2) { this.leftArray.push(this.rightArray.shift()) } }; /** * @description removes the first element from the queue and returns the value. If the queue is empty before the deletion, Then return 1 * @ return {number} * / FrontMiddleBackQueue prototype. PopFront = function () {/ / if the queue to return 1 if (! this.rightArray.length) return -1; // If the length of leftArray is 0, return the first element of rightArray if (! This.leftarray.length) return this.rightarray.shift () // Let ret = this.leftarray.shift () if there are at least two elements (this.leftarray.length + 1 < this.rightarray.length) {// The right side is too long, This.leftarray.push (this.rightarray.shift ())} return ret}; /** * @description removes the middle element from the queue and returns the value. If the queue is empty before deletion, Then return 1 * @ return {number} * / FrontMiddleBackQueue prototype. PopMiddle = function () {if (! this.rightArray.length) return -1; if (! this.leftArray.length) return this.rightArray.shift() if (this.rightArray.length == this.leftArray.length) return this.leftArray.pop() return this.rightArray.shift() }; /** * @description removes the last element from the queue and returns the value. If the queue is empty before deletion, Then return 1 * @ return {number} * / FrontMiddleBackQueue prototype. PopBack = function () {if (! this.rightArray.length) return -1; if (! this.leftArray.length) return this.rightArray.shift() let ret = this.rightArray.pop() if (this.rightArray.length < this.leftArray.length) { this.rightArray.unshift(this.leftArray.pop()) } return ret }; /** * Your FrontMiddleBackQueue object will be instantiated and called as such: * var obj = new FrontMiddleBackQueue() * obj.pushFront(val) * obj.pushMiddle(val) * obj.pushBack(val) * var param_4 = obj.popFront() * var param_5 = obj.popMiddle() * var param_6 = obj.popBack() */ // @lc code=endCopy the code