Leetcode 1418: Display Table of Food Orders in a Restaurant

grid47
grid47
Exploring patterns and algorithms
Jun 18, 2024 6 min read

Given a list of customer orders in a restaurant, return a table where each row represents a table number and the columns represent the number of food items ordered at that table, sorted in lexicographical order.
Problem
Approach
Steps
Complexity
Input: The input is a list of lists, where each sublist contains a customer name, table number, and food item.
Example: orders = [["John", "2", "Burger"], ["Jane", "1", "Pasta"], ["David", "3", "Pizza"], ["John", "2", "Pasta"], ["Jane", "3", "Pizza"]]
Constraints:
• 1 <= orders.length <= 5 * 10^4
• orders[i].length == 3
• 1 <= customerName.length, foodItem.length <= 20
• customerName and foodItem consist of lowercase and uppercase English letters and spaces.
• tableNumber is an integer between 1 and 500
Output: The output is a 2D list representing the display table. The first row is a header, with subsequent rows showing the number of orders for each food item at each table.
Example: [["Table", "Burger", "Pasta", "Pizza"], ["1", "0", "1", "0"], ["2", "1", "1", "0"], ["3", "0", "0", "2"]]
Constraints:
• The output is a 2D list.
Goal: To count the number of orders of each food item at each table and return them in a sorted manner.
Steps:
• Iterate over the orders and store the counts of each food item per table using a map.
• Sort the food items alphabetically and tables in increasing order.
• Construct the output list with table numbers and their respective food item counts.
Goal: The problem ensures that input size is manageable for the given constraints.
Steps:
• orders.length <= 5 * 10^4
• tableNumber between 1 and 500
Assumptions:
• All orders have a valid table number and food item.
Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"], ["David","3","Fried Chicken"], ["Carla","5","Water"]]
Explanation: For table 3, David orders Ceviche and Fried Chicken, and for table 5, Carla orders Water.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus