Leetcode 1418— Display Table of Food Orders in a Restaurant
1 min readApr 11, 2022
Statement
Given the array
orders
, which represents the orders that customers have done in a restaurant. More specificallyorders[i]=[customerName, tableNumber, foodItem]
wherecustomerName
is the name of the customer,tableNumber
is the table customer sit at, andfoodItem
is the item customer orders.
- Input format
‣ We have given a List of sublists where each sublist consists of 3 items. [customerName, tableNumber, foodItem].
‣ Each sublist has unique customerName, while tableNumber and foodItem can be repeated. - Output format
‣ Expected output is a List of sublists where first sublist indicates headers including food items names.
‣ Every sublist is uniquely identified by tableNumber, and all the rows are sorted by increasing sequence of tableNumber
Approach:
1. Iterate over orders(list) and store a sorted list of unique food items.
2. Unpack entities of each orders by iterating over list and check if food item is ordered by table. Use dictionary to calculate and store frequency of food items.
#initial frequency is 0 for each food item
final_dict = {tableNumber: [0, 0, 0, 0]}#length of value is same as number of unique food items
final_dict[i] = ['0'] * len(food_item_list)
3. Return the output in required form
#final result
results = [
['Table', 'food1', 'food2', 'food3', 'food4'],
[tableNumber, food1count, food2count, food3count, food4count],
....]
Code
Thanks!!