Problem Description
Given a list of orders where each order is represented as [customerName, tableNumber, foodItem], construct and return a "display table" of the restaurant. The display table is a 2D table where the first row is a header row with "Table" as the first column followed by the list of food items (in alphabetical order). Each subsequent row corresponds to a table (sorted numerically) and shows the counts of each food item ordered at that table.
Key Insights
- Use a set to gather all unique food items, then sort them alphabetically.
- Use a hashmap/dictionary to map table numbers to their counts of food items.
- Since table numbers are given as strings, sort them after converting to integers.
- Build the final display table with the header row and then each row corresponding to a specific table.
Space and Time Complexity
Time Complexity: O(N log N + F log F) where N is the number of orders and F is the number of unique food items. Space Complexity: O(N + F), to store the orders mapped by table and the unique food items.
Solution
We iterate over each order, storing the food items in a set to deduce the header and updating a mapping for each table with the counts of food items. After processing, we sort the food items alphabetically and the table numbers numerically. Finally, we build the display table by first adding the header row, then for each table, add a row containing the table number followed by counts (defaults to zero if not ordered) for each food item.