1418. 点菜展示表
为保证权益,题目请参考 1418. 点菜展示表(From LeetCode).
解决方案1
Python
python
from typing import List
import functools
class Solution:
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
foodNames = set()
tableNumbers = set()
data = dict()
for customerName, tableNumber, foodName in orders:
foodNames.add(foodName)
tableNumbers.add(tableNumber)
if tableNumber in data:
if foodName in data[tableNumber]:
data[tableNumber][foodName] += 1
else:
data[tableNumber][foodName] = 1
else:
data[tableNumber] = {foodName: 1}
# sort
foodNames = sorted(list(foodNames))
tableNumbers = sorted(
list(tableNumbers), key=functools.cmp_to_key(lambda x, y: int(x) - int(y))
)
ans = []
ans.append(["Table", *foodNames])
for tableNumber in tableNumbers:
foodNumbers = []
for foodName in foodNames:
if foodName in data[tableNumber]:
foodNumbers.append(str(data[tableNumber][foodName]))
else:
foodNumbers.append("0")
ans.append([tableNumber, *foodNumbers])
return ans
if __name__ == "__main__":
so = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44