Similar Problems

Similar Problems not available

Display Table Of Food Orders In A Restaurant - Leetcode Solution

Companies:

LeetCode:  Display Table Of Food Orders In A Restaurant Leetcode Solution

Difficulty: Medium

Topics: string hash-table sorting array  

The problem "Display Table of Food Orders in a Restaurant" on LeetCode requires us to display a table listing the food orders in a restaurant. The given input is a list of strings, where each string represents an order in the restaurant. Each order is in the following format:

"{customerName} {tableNumber} {orderItem}"

Our task is to parse this input and generate a table that displays the count of each menu item for each table in the restaurant, sorted in ascending order by table number.

We can approach this problem using a dictionary to store the data for each table and menu item. We can iterate over each order and extract the relevant data to update our dictionary.

Here's the step-by-step solution to this problem:

  1. Create an empty dictionary to store the data for each table and menu item:

    table_dict = {}
    
  2. Iterate over each order in the input list:

    for order in orders:
        customer_name, table_number, order_item = order.split()
    
  3. Check if the table number is already present in our dictionary. If not, initialize it with an empty dictionary:

    if table_number not in table_dict:
        table_dict[table_number] = {}
    
  4. Check if the order item is already present in the table's dictionary. If not, initialize it with a count of zero:

    if order_item not in table_dict[table_number]:
        table_dict[table_number][order_item] = 0
    
  5. Increment the count of the order item for the table:

    table_dict[table_number][order_item] += 1
    
  6. After iterating over all orders, we have our table data in the table_dict. We can generate the output table by iterating over each table's data and sorting it by menu item name:

    output = []
    
    for table_number, table_data in sorted(table_dict.items(), key=lambda x: int(x[0])):
        row = [table_number]
        
        for menu_item in sorted(table_data.keys()):
            row.append(str(table_data[menu_item]))
            
        output.append(row)
    
  7. The final output is a list of lists representing the table, each containing the table number and the count of each menu item in that table.

Here's the complete code for the above solution:

class Solution:
    def displayTable(self, orders: List[str]) -> List[List[str]]:
        table_dict = {}
        
        for order in orders:
            customer_name, table_number, order_item = order.split()
            
            if table_number not in table_dict:
                table_dict[table_number] = {}
                
            if order_item not in table_dict[table_number]:
                table_dict[table_number][order_item] = 0
                
            table_dict[table_number][order_item] += 1
        
        output = []
        
        # Generate table header
        header_row = ['Table']
        menu_items = set()
        for table_data in table_dict.values():
            menu_items.update(table_data.keys())
        header_row.extend(sorted(menu_items))
        output.append(header_row)
        
        # Generate table rows
        for table_number, table_data in sorted(table_dict.items(), key=lambda x: int(x[0])):
            row = [table_number]
            for menu_item in header_row[1:]:
                if menu_item in table_data:
                    row.append(str(table_data[menu_item]))
                else:
                    row.append('0')
            output.append(row)
        
        return output

This solution uses a set to keep track of all unique menu items and a lambda function to sort the table data by table number. This should provide an optimal time complexity of O(n log n), where n is the number of orders.

Display Table Of Food Orders In A Restaurant Solution Code

1