请你设计一个队列,支持在前,中,后三个位置的 push
和 pop
操作。
请你完成 FrontMiddleBack
类:
FrontMiddleBack()
初始化队列。void pushFront(int val)
将val
添加到队列的 最前面 。void pushMiddle(int val)
将val
添加到队列的 正中间 。void pushBack(int val)
将val
添加到队里的 最后面 。int popFront()
将 最前面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回-1
。int popMiddle()
将 正中间 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回-1
。int popBack()
将 最后面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回-1
。
请注意当有 两个 中间位置的时候,选择靠前面的位置进行操作。比方说:
- 将
6
添加到[1, 2, 3, 4, 5]
的中间位置,结果数组为[1, 2, 6, 3, 4, 5]
。 - 从
[1, 2, 3, 4, 5, 6]
的中间位置弹出元素,返回3
,数组变为[1, 2, 4, 5, 6]
。
示例 1:
输入: ["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", "popFront", "popMiddle", "popMiddle", "popBack", "popFront"] [[], [1], [2], [3], [4], [], [], [], [], []] 输出: [null, null, null, null, null, 1, 3, 4, 2, -1] 解释: 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(); // 返回 1 -> [4, 3, 2] q.popMiddle(); // 返回 3 -> [4, 2] q.popMiddle(); // 返回 4 -> [2] q.popBack(); // 返回 2 -> [] q.popFront(); // 返回 -1 -> [] (队列为空)
提示:
1 <= val <= 109
- 最多调用
1000
次pushFront
,pushMiddle
,pushBack
,popFront
,popMiddle
和popBack
。
方法一:双“双端队列”
我们用两个双端队列,其中 rebalance
函数来维护两个队列的平衡性,即保持
时间复杂度方面,每次操作的时间复杂度为
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()
class FrontMiddleBackQueue {
private Deque<Integer> q1 = new ArrayDeque<>();
private Deque<Integer> q2 = new ArrayDeque<>();
public FrontMiddleBackQueue() {
}
public void pushFront(int val) {
q1.offerFirst(val);
rebalance();
}
public void pushMiddle(int val) {
q1.offerLast(val);
rebalance();
}
public void pushBack(int val) {
q2.offerLast(val);
rebalance();
}
public int popFront() {
if (q1.isEmpty() && q2.isEmpty()) {
return -1;
}
int val = q1.isEmpty() ? q2.pollFirst() : q1.pollFirst();
rebalance();
return val;
}
public int popMiddle() {
if (q1.isEmpty() && q2.isEmpty()) {
return -1;
}
int val = q1.size() == q2.size() ? q1.pollLast() : q2.pollFirst();
rebalance();
return val;
}
public int popBack() {
if (q2.isEmpty()) {
return -1;
}
int val = q2.pollLast();
rebalance();
return val;
}
private void rebalance() {
if (q1.size() > q2.size()) {
q2.offerFirst(q1.pollLast());
}
if (q2.size() > q1.size() + 1) {
q1.offerLast(q2.pollFirst());
}
}
}
/**
* Your FrontMiddleBackQueue object will be instantiated and called as such:
* FrontMiddleBackQueue obj = new FrontMiddleBackQueue();
* obj.pushFront(val);
* obj.pushMiddle(val);
* obj.pushBack(val);
* int param_4 = obj.popFront();
* int param_5 = obj.popMiddle();
* int param_6 = obj.popBack();
*/
class FrontMiddleBackQueue {
public:
FrontMiddleBackQueue() {
}
void pushFront(int val) {
q1.push_front(val);
rebalance();
}
void pushMiddle(int val) {
q1.push_back(val);
rebalance();
}
void pushBack(int val) {
q2.push_back(val);
rebalance();
}
int popFront() {
if (q1.empty() && q2.empty()) return -1;
int val = 0;
if (q1.size()) {
val = q1.front();
q1.pop_front();
} else {
val = q2.front();
q2.pop_front();
}
rebalance();
return val;
}
int popMiddle() {
if (q1.empty() && q2.empty()) return -1;
int val = 0;
if (q1.size() == q2.size()) {
val = q1.back();
q1.pop_back();
} else {
val = q2.front();
q2.pop_front();
}
rebalance();
return val;
}
int popBack() {
if (q2.empty()) return -1;
int val = q2.back();
q2.pop_back();
rebalance();
return val;
}
private:
deque<int> q1;
deque<int> q2;
void rebalance() {
if (q1.size() > q2.size()) {
q2.push_front(q1.back());
q1.pop_back();
}
if (q2.size() > q1.size() + 1) {
q1.push_back(q2.front());
q2.pop_front();
}
}
};
/**
* Your FrontMiddleBackQueue object will be instantiated and called as such:
* FrontMiddleBackQueue* obj = new FrontMiddleBackQueue();
* obj->pushFront(val);
* obj->pushMiddle(val);
* obj->pushBack(val);
* int param_4 = obj->popFront();
* int param_5 = obj->popMiddle();
* int param_6 = obj->popBack();
*/
type FrontMiddleBackQueue struct{}
var a []int
func Constructor() (_ FrontMiddleBackQueue) {
a = nil
return
}
func (FrontMiddleBackQueue) PushFront(v int) {
a = append([]int{v}, a...)
}
func (FrontMiddleBackQueue) PushMiddle(v int) {
p := len(a) / 2
a = append(a[:p], append([]int{v}, a[p:]...)...)
}
func (FrontMiddleBackQueue) PushBack(v int) {
a = append(a, v)
}
func (FrontMiddleBackQueue) PopFront() (ans int) {
if len(a) == 0 {
return -1
}
ans = a[0]
a = a[1:]
return
}
func (FrontMiddleBackQueue) PopMiddle() (ans int) {
if len(a) == 0 {
return -1
}
p := (len(a) - 1) / 2
ans = a[p]
a = append(a[:p], a[p+1:]...)
return
}
func (FrontMiddleBackQueue) PopBack() (ans int) {
if len(a) == 0 {
return -1
}
ans = a[len(a)-1]
a = a[:len(a)-1]
return
}
var FrontMiddleBackQueue = function () {
this.left = [];
this.right = [];
};
/**
* @param {number} val
* @return {void}
*/
FrontMiddleBackQueue.prototype.pushFront = function (val) {
this.left.unshift(val);
this.rebalance();
};
/**
* @param {number} val
* @return {void}
*/
FrontMiddleBackQueue.prototype.pushMiddle = function (val) {
this.left.push(val);
this.rebalance();
};
/**
* @param {number} val
* @return {void}
*/
FrontMiddleBackQueue.prototype.pushBack = function (val) {
this.right.push(val);
this.rebalance();
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popFront = function () {
if (this.isEmpty()) return -1;
let num = this.left.length == 0 ? this.right.shift() : this.left.shift();
this.rebalance();
return num;
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popMiddle = function () {
if (this.isEmpty()) return -1;
let num =
this.left.length == this.right.length
? this.left.pop()
: this.right.shift();
this.rebalance();
return num;
};
/**
* @return {number}
*/
FrontMiddleBackQueue.prototype.popBack = function () {
if (this.isEmpty()) return -1;
let num = this.right.pop();
this.rebalance();
return num;
};
FrontMiddleBackQueue.prototype.rebalance = function () {
while (this.left.length > this.right.length) {
this.right.unshift(this.left.pop());
}
while (this.right.length > this.left.length + 1) {
this.left.push(this.right.shift());
}
};
FrontMiddleBackQueue.prototype.isEmpty = function () {
return this.left.length == 0 && this.right.length == 0;
};
/**
* 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()
*/