Display Table of Food Orders in a Restaurant Problem
Description
LeetCode Problem 1418.
Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerName_i,tableNumber_i,foodItem_i] where customerName_i is the name of the customer, tableNumber_i is the table customer sit at, and foodItem_i is the item customer orders.
Return the restaurant's “display table”. The “display table” 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 “Table”, 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.
Example 1:
Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]
Explanation:
The displaying table looks like:
Table,Beef Burrito,Ceviche,Fried Chicken,Water
3 ,0 ,2 ,1 ,0
5 ,0 ,1 ,0 ,1
10 ,1 ,0 ,0 ,0
For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche".
For the table 5: Carla orders "Water" and "Ceviche".
For the table 10: Corina orders "Beef Burrito".
Example 2:
Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]]
Explanation:
For the table 1: Adam and Brianna order "Canadian Waffles".
For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
Example 3:
Input: orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]
Constraints:
1 <= orders.length <= 5 * 10^4orders[i].length == 31 <= customerName_i.length, foodItem_i.length <= 20customerName_iandfoodItem_iconsist of lowercase and uppercase English letters and the space character.tableNumber_iis a valid integer between1and500.
Solution
Approach: Count, Then Sort
Go through the orders once and record two things:
- for each table, how many times each food item was ordered (a map from table number to a map from food to count),
- the set of all food items.
Then build the table:
- The header is
"Table"followed by the food items in sorted order. - For each table, in increasing numeric order, add a row with the table number and, for every item in the header's order, its count at that table (
"0"if the table never ordered it), all as strings.
The detail to watch is the sort order of the tables: they must be sorted as numbers, so "10" comes after "5". Converting the table number to an integer before using it as a key takes care of that. The customer names are not needed at all.
Complexity: O(n + F log F + T log T + T · F) time for n orders, F distinct foods and T tables, and O(n + T · F) space.
from collections import Counter, defaultdict
class Solution:
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
counts = defaultdict(Counter) # table number -> food -> how many
foods = set()
for _, table, food in orders:
counts[int(table)][food] += 1
foods.add(food)
foods = sorted(foods)
ans = [["Table"] + foods]
for table in sorted(counts): # sort numerically, not as strings
ans.append([str(table)] + [str(counts[table][f]) for f in foods])
return ans