{"id":7725,"date":"2020-11-28T12:00:04","date_gmt":"2020-11-28T20:00:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7725"},"modified":"2020-11-28T12:25:53","modified_gmt":"2020-11-28T20:25:53","slug":"leetcode-1670-design-front-middle-back-queue","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-1670-design-front-middle-back-queue\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1670. Design Front Middle Back Queue"},"content":{"rendered":"\n<p>Design a queue that supports&nbsp;<code>push<\/code>&nbsp;and&nbsp;<code>pop<\/code>&nbsp;operations in the front, middle, and back.<\/p>\n\n\n\n<p>Implement the&nbsp;<code>FrontMiddleBack<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>FrontMiddleBack()<\/code>&nbsp;Initializes the queue.<\/li><li><code>void pushFront(int val)<\/code>&nbsp;Adds&nbsp;<code>val<\/code>&nbsp;to the&nbsp;<strong>front<\/strong>&nbsp;of the queue.<\/li><li><code>void pushMiddle(int val)<\/code>&nbsp;Adds&nbsp;<code>val<\/code>&nbsp;to the&nbsp;<strong>middle<\/strong>&nbsp;of the queue.<\/li><li><code>void pushBack(int val)<\/code>&nbsp;Adds&nbsp;<code>val<\/code>&nbsp;to the&nbsp;<strong>back<\/strong>&nbsp;of the queue.<\/li><li><code>int popFront()<\/code>&nbsp;Removes the&nbsp;<strong>front<\/strong>&nbsp;element of the queue and returns it. If the queue is empty, return&nbsp;<code>-1<\/code>.<\/li><li><code>int popMiddle()<\/code>&nbsp;Removes the&nbsp;<strong>middle<\/strong>&nbsp;element of the queue and returns it. If the queue is empty, return&nbsp;<code>-1<\/code>.<\/li><li><code>int popBack()<\/code>&nbsp;Removes the&nbsp;<strong>back<\/strong>&nbsp;element of the queue and returns it. If the queue is empty, return&nbsp;<code>-1<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Notice<\/strong>&nbsp;that when there are&nbsp;<strong>two<\/strong>&nbsp;middle position choices, the operation is performed on the&nbsp;<strong>frontmost<\/strong>&nbsp;middle position choice. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Pushing&nbsp;<code>6<\/code>&nbsp;into the middle of&nbsp;<code>[1, 2, 3, 4, 5]<\/code>&nbsp;results in&nbsp;<code>[1, 2,&nbsp;6, 3, 4, 5]<\/code>.<\/li><li>Popping the middle from&nbsp;<code>[1, 2,&nbsp;3, 4, 5, 6]<\/code>&nbsp;returns&nbsp;<code>3<\/code>&nbsp;and results in&nbsp;<code>[1, 2, 4, 5, 6]<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>\n[\"FrontMiddleBackQueue\", \"pushFront\", \"pushBack\", \"pushMiddle\", \"pushMiddle\", \"popFront\", \"popMiddle\", \"popMiddle\", \"popBack\", \"popFront\"]\n[[], [1], [2], [3], [4], [], [], [], [], []]\n<strong>Output:<\/strong>\n[null, null, null, null, null, 1, 3, 4, 2, -1]\n<strong>Explanation:<\/strong>\nFrontMiddleBackQueue q = new FrontMiddleBackQueue();\nq.pushFront(1);   \/\/ [1]\nq.pushBack(2);    \/\/ [1, 2]\nq.pushMiddle(3);  \/\/ [1, 3, 2]\nq.pushMiddle(4);  \/\/ [1, 4, 3, 2]\nq.popFront();     \/\/ return 1 -&gt; [4, 3, 2]\nq.popMiddle();    \/\/ return 3 -&gt; [4, 2]\nq.popMiddle();    \/\/ return 4 -&gt; [2]\nq.popBack();      \/\/ return 2 -&gt; []\nq.popFront();     \/\/ return -1 -&gt; [] (The queue is empty)\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= val &lt;= 10<sup>9<\/sup><\/code><\/li><li>At most&nbsp;<code>1000<\/code>&nbsp;calls will be made to&nbsp;<code>pushFront<\/code>,&nbsp;<code>pushMiddle<\/code>,&nbsp;<code>pushBack<\/code>,&nbsp;<code>popFront<\/code>,&nbsp;<code>popMiddle<\/code>, and&nbsp;<code>popBack<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: List + Middle Iterator<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(1) per op<br>Space complexity: O(n) in total<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass FrontMiddleBackQueue {\npublic:\n  FrontMiddleBackQueue() {}\n\n  void pushFront(int val) {\n    q_.push_front(val); \n    updateMit(even() ? -1 : 0);\n  }\n\n  void pushMiddle(int val) {    \n    if (q_.empty())\n      q_.push_back(val);\n    else\n      q_.insert(even() ? next(mit_) : mit_, val);\n    updateMit(even() ? -1 : 1);\n  }\n\n  void pushBack(int val) {    \n    q_.push_back(val); \n    updateMit(even() ? 0 : 1);\n  }\n\n  int popFront() {     \n    if (q_.empty()) return -1;\n    int val = q_.front();\n    q_.pop_front();\n    updateMit(even() ? 0 : 1);\n    return val;\n  }\n\n  int popMiddle() {    \n    if (q_.empty()) return -1;\n    int val = *mit_;\n    mit_ = q_.erase(mit_);\n    updateMit(even() ? -1 : 0);\n    return val;\n  }\n\n  int popBack() {    \n    if (q_.empty()) return -1;\n    int val = q_.back();\n    q_.pop_back();\n    updateMit(even() ? -1 : 0);\n    return val;\n  }\nprivate:  \n  void updateMit(int delta) {\n    if (q_.size() <= 1) {      \n      mit_ = begin(q_);\n    } else {\n      if (delta > 0) ++mit_;\n      if (delta < 0) --mit_;\n    }    \n  }\n  \n  bool even() const { return q_.size() % 2 == 0; }\n  \n  list<int> q_;\n  list<int>::iterator mit_;\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Design a queue that supports&nbsp;push&nbsp;and&nbsp;pop&nbsp;operations in the front, middle, and back. Implement the&nbsp;FrontMiddleBack&nbsp;class: FrontMiddleBack()&nbsp;Initializes the queue. void pushFront(int val)&nbsp;Adds&nbsp;val&nbsp;to the&nbsp;front&nbsp;of the queue. void pushMiddle(int val)&nbsp;Adds&nbsp;val&nbsp;to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[291,251,625,83],"class_list":["post-7725","post","type-post","status-publish","format-standard","hentry","category-list","tag-data-structure","tag-design","tag-iterator","tag-list","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7725"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7725\/revisions"}],"predecessor-version":[{"id":7728,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7725\/revisions\/7728"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}