[LeetCode] Design Front Middle Back Queue

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].
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class FrontMiddleBackQueue {
deque<int> f, b;
void balance() {
int s = f.size() + b.size();
if(s & 1) {
if(f.size() > b.size()) {
while(f.size() > b.size()) {
b.push_front(f.back());
f.pop_back();
}
} else if(b.size() > f.size()) {
while(f.size() + 1 != b.size()) {
f.push_back(b.front());
b.pop_front();
}
}
} else {
if(f.size() != b.size()) {
if(f.size() > b.size()) {
while(f.size() != b.size()) {
b.push_front(f.back());
f.pop_back();
}
} else {
while(f.size() != b.size()) {
f.push_back(b.front());
b.pop_front();
}
}
}
}
}
public:
FrontMiddleBackQueue() {}

void pushFront(int val) {
f.push_front(val);
balance();
}

void pushMiddle(int val) {
f.push_back(val);
balance();
}

void pushBack(int val) {
b.push_back(val);
balance();
}

int popFront() {
int res = -1;
if(!f.empty()) {
res = f.front();
f.pop_front();
} else if(!b.empty()) {
res = b.front();
b.pop_front();
}
balance();
return res;
}

int popMiddle() {
int s = f.size() + b.size();
int res = -1;
if(s != 0) {
if(s & 1) {
if(!b.empty()) {
res = b.front();
b.pop_front();
} else if(!f.empty()) {
res = f.back();
b.pop_back();
}
} else {
if(!f.empty()) {
res = f.back();
f.pop_back();
} else if(!b.empty()) {
res = b.front();
b.pop_front();
}
}
balance();
}
return res;
}

int popBack() {
int res = -1;
if(!b.empty()) {
res = b.back();
b.pop_back();
} else if(!f.empty()) {
res = f.back();
f.pop_back();
}
balance();
return res;
}
};

/**
* 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();
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/18/PS/LeetCode/design-front-middle-back-queue/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.