C++ two deques, O(1) for all
See my latest update in repo LeetCode\n\n## Solution 1. Two Deque\n\nUse two deque<int> a, b where a and b represent the first and second half of the numbers in
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1. Two Deque\n\nUse two `deque<int> a, b` where `a` and `b` represent the first and second half of the numbers in queue, respectively.\n\nMake sure `a.size()` be either `b.size()` or `b.size() - 1` so that when we `pushMiddle`, always push to `a`.\n\n`a2b` and `b2a` are two helper functions nudging numbers around to ensure the above criteria.\n\n```cpp\n// OJ: https://leetcode.com/contest/biweekly-contest-40/problems/design-front-middle-back-queue/\n// Author: github.com/lzl124631x\n// Time: O(1) for all\n// Space: O(N)\nclass FrontMiddleBackQueue {\n    deque<int> a, b;\n    void a2b() {\n        if (a.size() <= b.size()) return;\n        b.push_front(a.back());\n        a.pop_back();\n    }\n    void b2a() {\n        if (b.size() <= a.size() + 1) return;\n        a.push_back(b.front());\n        b.pop_front();\n    }\npublic:\n    FrontMiddleBackQueue() {}\n    void pushFront(int val) {\n        a.push_front(val);\n        a2b();\n    }\n    void pushMiddle(int val) {\n        a.push_back(val);\n        a2b();\n    }\n    void pushBack(int val) {\n        b.push_back(val);\n        b2a();\n    }\n    int popFront() {\n        if (a.empty() && b.empty()) return -1;\n        int ans;\n        if (a.empty()) {\n            ans = b.front();\n            b.pop_front();\n        } else {\n            ans = a.front();\n            a.pop_front();\n            b2a();\n        }\n        return ans;\n    }\n    int popMiddle() {\n        if (a.empty() && b.empty()) return -1;\n        int ans;\n        if (a.size() == b.size()) {\n            ans = a.back();\n            a.pop_back();\n        } else {\n            ans = b.front();\n            b.pop_front();\n        }\n        return ans;\n    }\n    int popBack() {\n        if (a.empty() && b.empty()) return -1;\n        int ans = b.back();\n        b.pop_back();\n        a2b();\n        return ans;\n    }\n};\n```
[]