LeetCode contest 185 1418. 点菜展示表

简介: LeetCode contest 185 1418. 点菜展示表

LeetCode contest 185 1418. 点菜展示表


Table of Contents

一、中文版

二、英文版

三、My answer

四、解题报告


一、中文版

给你一个数组 orders,表示客户在餐厅中完成的订单,确切地说, orders[i]=[customerNamei,tableNumberi,foodItemi] ,其中 customerNamei 是客户的姓名,tableNumberi 是客户所在餐桌的桌号,而 foodItemi 是客户点的餐品名称。


请你返回该餐厅的 点菜展示表 。在这张表中,表中第一行为标题,其第一列为餐桌桌号 “Table” ,后面每一列都是按字母顺序排列的餐品名称。接下来每一行中的项则表示每张餐桌订购的相应餐品数量,第一列应当填对应的桌号,后面依次填写下单的餐品数量。


注意:客户姓名不是点菜展示表的一部分。此外,表中的数据行应该按餐桌桌号升序排列。



示例 1:


输入:orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]

输出:[["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]

解释:

点菜展示表如下所示:

Table,Beef Burrito,Ceviche,Fried Chicken,Water

3    ,0           ,2      ,1            ,0

5    ,0           ,1      ,0            ,1

10   ,1           ,0      ,0            ,0

对于餐桌 3:David 点了 "Ceviche" 和 "Fried Chicken",而 Rous 点了 "Ceviche"

而餐桌 5:Carla 点了 "Water" 和 "Ceviche"

餐桌 10:Corina 点了 "Beef Burrito"

示例 2:


输入:orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]

输出:[["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]]

解释:

对于餐桌 1:Adam 和 Brianna 都点了 "Canadian Waffles"

而餐桌 12:James, Ratesh 和 Amadeus 都点了 "Fried Chicken"

示例 3:


输入:orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]

输出:[["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]


提示:


1 <= orders.length <= 5 * 10^4

orders[i].length == 3

1 <= customerNamei.length, foodItemi.length <= 20

customerNamei 和 foodItemi 由大小写英文字母及空格字符 ' ' 组成。

tableNumberi 是 1 到 500 范围内的整数。


二、英文版

Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders.
Return the restaurant's “display table”. The “display table” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.
Example 1:
Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["David","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]] 
Explanation:
The displaying table looks like:
Table,Beef Burrito,Ceviche,Fried Chicken,Water
3    ,0           ,2      ,1            ,0
5    ,0           ,1      ,0            ,1
10   ,1           ,0      ,0            ,0
For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "Ceviche".
For the table 5: Carla orders "Water" and "Ceviche".
For the table 10: Corina orders "Beef Burrito". 
Example 2:
Input: orders = [["James","12","Fried Chicken"],["Ratesh","12","Fried Chicken"],["Amadeus","12","Fried Chicken"],["Adam","1","Canadian Waffles"],["Brianna","1","Canadian Waffles"]]
Output: [["Table","Canadian Waffles","Fried Chicken"],["1","2","0"],["12","0","3"]] 
Explanation: 
For the table 1: Adam and Brianna order "Canadian Waffles".
For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
Example 3:
Input: orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
Output: [["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]
Constraints:
1 <= orders.length <= 5 * 10^4
orders[i].length == 3
1 <= customerNamei.length, foodItemi.length <= 20
customerNamei and foodItemi consist of lowercase and uppercase English letters and the space character.
tableNumberi is a valid integer between 1 and 500.


三、My answer

class Solution:
    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
        table = set()
        dish = set()
        dict_ = {}
        for order in orders:
        # order[1]是桌号,order[2]的菜名
            table.add(int(order[1]))
            dish.add(order[2])
            if order[1] not in dict_:
                dict_[order[1]] = collections.defaultdict(lambda: 0)
            if order[2] not in dict_[order[1]]:
                dict_[order[1]][order[2]] = 0
            dict_[order[1]][order[2]] += 1
        table = sorted(list(table))
        dish = sorted(list(dish))
        res = [['' for i in range(len(dish)+1)] for i in range(len(table) + 1)]
        res[0][0] = 'Table'
        # 下面这个 for 循环是最后结果的第一行表头
        for j in range(1,len(dish)+1):
            res[0][j] = dish[j-1]
        # 下面这个双重 for 循环填满 res 的每个值
        for i in range(1,len(res)):
            res[i][0] = str(table[i-1])
            for j in range(1,len(dish)+1):
                res[i][j] = str(dict_[res[i][0]][res[0][j]])
        return res


四、解题报告


个人认为,本题较有难度的地方是如何存储最后输出结果需要的内容。


本题中用到的三个容器打印出来的形式如下:


table: [3, 5, 10]

dish: ['Beef Burrito', 'Ceviche', 'Fried Chicken', 'Water']

dict_: {'3': defaultdict(<function Solution.displayTable.<locals>.<lambda> at 0x7f5e6b133430>, {'Ceviche': 2, 'Fried Chicken': 1}), '10': defaultdict(<function Solution.displayTable.<locals>.<lambda> at 0x7f5e6b1334c0>, {'Beef Burrito': 1}), '5': defaultdict(<function Solution.displayTable.<locals>.<lambda> at 0x7f5e6b133550>, {'Water': 1, 'Ceviche': 1})}

相关文章
|
6月前
|
SQL 算法 vr&ar
☆打卡算法☆LeetCode 175. 组合两个表 算法解析
☆打卡算法☆LeetCode 175. 组合两个表 算法解析
|
JavaScript 前端开发
leetcode 1418.点菜展示表(JavaScript)
leetcode 1418.点菜展示表(JavaScript)
42 0
leetcode-每日一题731. 我的日程安排表 II
题目需要我们判断在重复的预定时间里,有三重预定的返回false,那我们可以定义一个pair结构体用来表示时间段,MyCalendarTwo结构体用来存成功预定的时间段booked切片,和有重复预定的时间段overlaps切片,overlaps切片用来判断新进的时间段是否跟overlaps有重合预定的情况,若有则返回false,没有则true
109 0
leetcode-每日一题731. 我的日程安排表 II
leetcode-每日一题729. 我的日程安排表 I
我们把安排成功的日程插入到日历切片里,Book方法只需要遍历日历切片,如果存在时间交叉的日程则直接返回 false, 没有则将日程插入到日历切片当中,返回true
74 0
leetcode-每日一题729. 我的日程安排表 I
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 190 5418. 二叉树中的伪回文路径 Pseudo-Palindromic Paths in a Binary Tree
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 200 5475. 统计好三元组 Count Good Triplets
LeetCode contest 199 灯泡开关 IV Bulb Switcher IV
LeetCode contest 199 灯泡开关 IV Bulb Switcher IV
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length