{"id":6646,"date":"2020-04-20T00:32:06","date_gmt":"2020-04-20T07:32:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6646"},"modified":"2020-04-20T00:32:42","modified_gmt":"2020-04-20T07:32:42","slug":"leetcode-1418-display-table-of-food-orders-in-a-restaurant","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1418-display-table-of-food-orders-in-a-restaurant\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1418. Display Table of Food Orders in a Restaurant"},"content":{"rendered":"\n<p>Given&nbsp;the array&nbsp;<code>orders<\/code>, which represents the orders that customers have done in a restaurant. More specifically&nbsp;<code>orders[i]=[customerName<sub>i<\/sub>,tableNumber<sub>i<\/sub>,foodItem<sub>i<\/sub>]<\/code>&nbsp;where&nbsp;<code>customerName<sub>i<\/sub><\/code>&nbsp;is the name of the customer,&nbsp;<code>tableNumber<sub>i<\/sub><\/code>&nbsp;is the table customer sit at, and&nbsp;<code>foodItem<sub>i<\/sub><\/code>&nbsp;is the item customer orders.<\/p>\n\n\n\n<p><em>Return the restaurant&#8217;s \u201c<strong>display table<\/strong>\u201d<\/em>. The \u201c<strong>display table<\/strong>\u201d is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is \u201cTable\u201d, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> orders = [[\"David\",\"3\",\"Ceviche\"],[\"Corina\",\"10\",\"Beef Burrito\"],[\"David\",\"3\",\"Fried Chicken\"],[\"Carla\",\"5\",\"Water\"],[\"Carla\",\"5\",\"Ceviche\"],[\"Rous\",\"3\",\"Ceviche\"]]\n<strong>Output:<\/strong> [[\"Table\",\"Beef Burrito\",\"Ceviche\",\"Fried Chicken\",\"Water\"],[\"3\",\"0\",\"2\",\"1\",\"0\"],[\"5\",\"0\",\"1\",\"0\",\"1\"],[\"10\",\"1\",\"0\",\"0\",\"0\"]] \n<strong>Explanation:\n<\/strong>The displaying table looks like:\n<strong>Table,Beef Burrito,Ceviche,Fried Chicken,Water<\/strong>\n3    ,0           ,2      ,1            ,0\n5    ,0           ,1      ,0            ,1\n10   ,1           ,0      ,0            ,0\nFor the table 3: David orders \"Ceviche\" and \"Fried Chicken\", and Rous orders \"Ceviche\".\nFor the table 5: Carla orders \"Water\" and \"Ceviche\".\nFor the table 10: Corina orders \"Beef Burrito\". \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> orders = [[\"James\",\"12\",\"Fried Chicken\"],[\"Ratesh\",\"12\",\"Fried Chicken\"],[\"Amadeus\",\"12\",\"Fried Chicken\"],[\"Adam\",\"1\",\"Canadian Waffles\"],[\"Brianna\",\"1\",\"Canadian Waffles\"]]\n<strong>Output:<\/strong> [[\"Table\",\"Canadian Waffles\",\"Fried Chicken\"],[\"1\",\"2\",\"0\"],[\"12\",\"0\",\"3\"]] \n<strong>Explanation:<\/strong> \nFor the table 1: Adam and Brianna order \"Canadian Waffles\".\nFor the table 12: James, Ratesh and Amadeus order \"Fried Chicken\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> orders = [[\"Laura\",\"2\",\"Bean Burrito\"],[\"Jhon\",\"2\",\"Beef Burrito\"],[\"Melissa\",\"2\",\"Soda\"]]\n<strong>Output:<\/strong> [[\"Table\",\"Bean Burrito\",\"Beef Burrito\",\"Soda\"],[\"2\",\"1\",\"1\",\"1\"]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;=&nbsp;orders.length &lt;= 5 * 10^4<\/code><\/li><li><code>orders[i].length == 3<\/code><\/li><li><code>1 &lt;= customerName<sub>i<\/sub>.length, foodItem<sub>i<\/sub>.length &lt;= 20<\/code><\/li><li><code>customerName<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>foodItem<sub>i<\/sub><\/code>&nbsp;consist of lowercase and uppercase English letters and the space character.<\/li><li><code>tableNumber<sub>i<\/sub>&nbsp;<\/code>is a valid integer between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>500<\/code>.<\/li><\/ul>\n\n\n\n<p>Solution: TreeMap\/Set + HashTable<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/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 Solution {\npublic:\n  vector<vector<string>> displayTable(vector<vector<string>>& orders) {\n    map<int, unordered_map<string, int>> tables;\n    set<string> foods;\n    for (const auto& order : orders) {\n      ++tables[stoi(order[1])][order[2]];\n      foods.insert(order[2]);\n    }\n    vector<vector<string>> ans;\n    ans.push_back({{\"Table\"}});    \n    ans.back().insert(end(ans.back()), begin(foods), end(foods));\n    for (auto& [table, m] : tables) {\n      vector<string> line{to_string(table)};\n      for (const auto& food : foods)\n        line.push_back(to_string(m[food]));\n      ans.push_back(move(line));\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given&nbsp;the array&nbsp;orders, which represents the orders that customers have done in a restaurant. More specifically&nbsp;orders[i]=[customerNamei,tableNumberi,foodItemi]&nbsp;where&nbsp;customerNamei&nbsp;is the name of the customer,&nbsp;tableNumberi&nbsp;is the table customer sit at,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,462,177,243],"class_list":["post-6646","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-map","tag-medium","tag-set","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6646","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=6646"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6646\/revisions"}],"predecessor-version":[{"id":6648,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6646\/revisions\/6648"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}