LeetCode 1418: Display Table of Food Orders in a Restaurant Solution
Master LeetCode problem 1418 (Display Table of Food Orders in a Restaurant), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1418. Display Table of Food Orders in a Restaurant
Problem Explanation
Explanation
To solve this problem, we need to iterate over the orders and create a mapping of table numbers to the count of each food item ordered by that table. We can achieve this by using a hashmap where the key is the table number and the value is another hashmap that maps food items to their count.
- Create a hashmap
tableOrdersto store the mapping of table numbers to the count of each food item ordered. - Iterate through the orders and update the
tableOrdershashmap accordingly. - Extract the unique food items from all orders and sort them in alphabetical order.
- Create the result table, with the first row containing the food items in alphabetical order.
- Iterate through the table numbers in sorted order and populate the counts of food items for each table accordingly.
Time complexity: O(n * m) where n is the number of orders and m is the average number of unique food items per order. Space complexity: O(n * m) for the hashmap storing the table orders. Java:
Solution Code
class Solution {
public List<List<String>> displayTable(List<List<String>> orders) {
Map<Integer, Map<String, Integer>> tableOrders = new TreeMap<>();
Set<String> foodItems = new TreeSet<>();
for (List<String> order : orders) {
int tableNum = Integer.parseInt(order.get(1));
String foodItem = order.get(2);
foodItems.add(foodItem);
tableOrders.putIfAbsent(tableNum, new TreeMap<>());
Map<String, Integer> foodCount = tableOrders.get(tableNum);
foodCount.put(foodItem, foodCount.getOrDefault(foodItem, 0) + 1);
}
List<List<String>> result = new ArrayList<>();
List<String> header = new ArrayList<>();
header.add("Table");
header.addAll(foodItems);
result.add(header);
for (Map.Entry<Integer, Map<String, Integer>> entry : tableOrders.entrySet()) {
List<String> row = new ArrayList<>();
row.add(String.valueOf(entry.getKey()));
for (String foodItem : foodItems) {
row.add(String.valueOf(entry.getValue().getOrDefault(foodItem, 0)));
}
result.add(row);
}
return result;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1418 (Display Table of Food Orders in a Restaurant)?
This page provides optimized solutions for LeetCode problem 1418 (Display Table of Food Orders in a Restaurant) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1418 (Display Table of Food Orders in a Restaurant)?
The time complexity for LeetCode 1418 (Display Table of Food Orders in a Restaurant) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1418 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1418 in Java, C++, or Python.