Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a69ee54

Browse files
authored
feat: add solutions to lc problem: No.1418 (doocs#3428)
No.1418.Display Table of Food Orders in a Restaurant
1 parent 1b973e2 commit a69ee54

File tree

7 files changed

+394
-312
lines changed

7 files changed

+394
-312
lines changed

solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/README.md

Lines changed: 137 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tags:
3333
<p><strong>示例 1:</strong></p>
3434

3535
<pre><strong>输入:</strong>orders = [[&quot;David&quot;,&quot;3&quot;,&quot;Ceviche&quot;],[&quot;Corina&quot;,&quot;10&quot;,&quot;Beef Burrito&quot;],[&quot;David&quot;,&quot;3&quot;,&quot;Fried Chicken&quot;],[&quot;Carla&quot;,&quot;5&quot;,&quot;Water&quot;],[&quot;Carla&quot;,&quot;5&quot;,&quot;Ceviche&quot;],[&quot;Rous&quot;,&quot;3&quot;,&quot;Ceviche&quot;]]
36-
<strong>输出:</strong>[[&quot;Table&quot;,&quot;Beef Burrito&quot;,&quot;Ceviche&quot;,&quot;Fried Chicken&quot;,&quot;Water&quot;],[&quot;3&quot;,&quot;0&quot;,&quot;2&quot;,&quot;1&quot;,&quot;0&quot;],[&quot;5&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;],[&quot;10&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]]
36+
<strong>输出:</strong>[[&quot;Table&quot;,&quot;Beef Burrito&quot;,&quot;Ceviche&quot;,&quot;Fried Chicken&quot;,&quot;Water&quot;],[&quot;3&quot;,&quot;0&quot;,&quot;2&quot;,&quot;1&quot;,&quot;0&quot;],[&quot;5&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;],[&quot;10&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]]
3737
<strong>解释:
3838
</strong>点菜展示表如下所示:
3939
<strong>Table,Beef Burrito,Ceviche,Fried Chicken,Water</strong>
@@ -42,13 +42,13 @@ tags:
4242
10 ,1 ,0 ,0 ,0
4343
对于餐桌 3:David 点了 &quot;Ceviche&quot;&quot;Fried Chicken&quot;,而 Rous 点了 &quot;Ceviche&quot;
4444
而餐桌 5:Carla 点了 &quot;Water&quot;&quot;Ceviche&quot;
45-
餐桌 10:Corina 点了 &quot;Beef Burrito&quot;
45+
餐桌 10:Corina 点了 &quot;Beef Burrito&quot;
4646
</pre>
4747

4848
<p><strong>示例 2:</strong></p>
4949

5050
<pre><strong>输入:</strong>orders = [[&quot;James&quot;,&quot;12&quot;,&quot;Fried Chicken&quot;],[&quot;Ratesh&quot;,&quot;12&quot;,&quot;Fried Chicken&quot;],[&quot;Amadeus&quot;,&quot;12&quot;,&quot;Fried Chicken&quot;],[&quot;Adam&quot;,&quot;1&quot;,&quot;Canadian Waffles&quot;],[&quot;Brianna&quot;,&quot;1&quot;,&quot;Canadian Waffles&quot;]]
51-
<strong>输出:</strong>[[&quot;Table&quot;,&quot;Canadian Waffles&quot;,&quot;Fried Chicken&quot;],[&quot;1&quot;,&quot;2&quot;,&quot;0&quot;],[&quot;12&quot;,&quot;0&quot;,&quot;3&quot;]]
51+
<strong>输出:</strong>[[&quot;Table&quot;,&quot;Canadian Waffles&quot;,&quot;Fried Chicken&quot;],[&quot;1&quot;,&quot;2&quot;,&quot;0&quot;],[&quot;12&quot;,&quot;0&quot;,&quot;3&quot;]]
5252
<strong>解释:</strong>
5353
对于餐桌 1:Adam 和 Brianna 都点了 &quot;Canadian Waffles&quot;
5454
而餐桌 12:James, Ratesh 和 Amadeus 都点了 &quot;Fried Chicken&quot;
@@ -78,7 +78,19 @@ tags:
7878

7979
<!-- solution:start -->
8080

81-
### 方法一
81+
### 方法一:哈希表 + 排序
82+
83+
我们可以用一个哈希表 $\textit{tables}$ 来存储每张餐桌点的菜品,用一个集合 $\textit{items}$ 来存储所有的菜品。
84+
85+
遍历 $\textit{orders}$,将每张餐桌点的菜品存入 $\textit{tables}$ 和 $\textit{items}$ 中。
86+
87+
然后我们将 $\textit{items}$ 排序,得到 $\textit{sortedItems}$。
88+
89+
接下来,我们构建答案数组 $\textit{ans}$,首先将标题行 $\textit{header}$ 加入 $\textit{ans}$,然后遍历排序后的 $\textit{tables}$,对于每张餐桌,我们用一个计数器 $\textit{cnt}$ 来统计每种菜品的数量,然后构建一行 $\textit{row}$,将其加入 $\textit{ans}$。
90+
91+
最后返回 $\textit{ans}$。
92+
93+
时间复杂度 $O(n + m \times \log m + k \times \log k + m \times k)$,空间复杂度 $O(n + m + k)$。其中 $n$ 是数组 $\textit{orders}$ 的长度,而 $m$ 和 $k$ 分别表示菜品种类数和餐桌数。
8294

8395
<!-- tabs:start -->
8496

@@ -87,58 +99,53 @@ tags:
8799
```python
88100
class Solution:
89101
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
90-
tables = set()
91-
foods = set()
92-
mp = Counter()
93-
for _, table, food in orders:
94-
tables.add(int(table))
95-
foods.add(food)
96-
mp[f'{table}.{food}'] += 1
97-
foods = sorted(list(foods))
98-
tables = sorted(list(tables))
99-
res = [['Table'] + foods]
100-
for table in tables:
101-
t = [str(table)]
102-
for food in foods:
103-
t.append(str(mp[f'{table}.{food}']))
104-
res.append(t)
105-
return res
102+
tables = defaultdict(list)
103+
items = set()
104+
for _, table, foodItem in orders:
105+
tables[int(table)].append(foodItem)
106+
items.add(foodItem)
107+
sorted_items = sorted(items)
108+
ans = [["Table"] + sorted_items]
109+
for table in sorted(tables):
110+
cnt = Counter(tables[table])
111+
row = [str(table)] + [str(cnt[item]) for item in sorted_items]
112+
ans.append(row)
113+
return ans
106114
```
107115

108116
#### Java
109117

110118
```java
111119
class Solution {
112120
public List<List<String>> displayTable(List<List<String>> orders) {
113-
Set<Integer> tables = new HashSet<>();
114-
Set<String> foods = new HashSet<>();
115-
Map<String, Integer> mp = new HashMap<>();
116-
for (List<String> order : orders) {
117-
int table = Integer.parseInt(order.get(1));
118-
String food = order.get(2);
119-
tables.add(table);
120-
foods.add(food);
121-
String key = table + "." + food;
122-
mp.put(key, mp.getOrDefault(key, 0) + 1);
121+
TreeMap<Integer, List<String>> tables = new TreeMap<>();
122+
Set<String> items = new HashSet<>();
123+
for (List<String> o : orders) {
124+
int table = Integer.parseInt(o.get(1));
125+
String foodItem = o.get(2);
126+
tables.computeIfAbsent(table, k -> new ArrayList<>()).add(foodItem);
127+
items.add(foodItem);
123128
}
124-
List<Integer> t = new ArrayList<>(tables);
125-
List<String> f = new ArrayList<>(foods);
126-
Collections.sort(t);
127-
Collections.sort(f);
128-
List<List<String>> res = new ArrayList<>();
129-
List<String> title = new ArrayList<>();
130-
title.add("Table");
131-
title.addAll(f);
132-
res.add(title);
133-
for (int table : t) {
134-
List<String> tmp = new ArrayList<>();
135-
tmp.add(String.valueOf(table));
136-
for (String food : f) {
137-
tmp.add(String.valueOf(mp.getOrDefault(table + "." + food, 0)));
129+
List<String> sortedItems = new ArrayList<>(items);
130+
Collections.sort(sortedItems);
131+
List<List<String>> ans = new ArrayList<>();
132+
List<String> header = new ArrayList<>();
133+
header.add("Table");
134+
header.addAll(sortedItems);
135+
ans.add(header);
136+
for (Map.Entry<Integer, List<String>> entry : tables.entrySet()) {
137+
Map<String, Integer> cnt = new HashMap<>();
138+
for (String item : entry.getValue()) {
139+
cnt.merge(item, 1, Integer::sum);
140+
}
141+
List<String> row = new ArrayList<>();
142+
row.add(String.valueOf(entry.getKey()));
143+
for (String item : sortedItems) {
144+
row.add(String.valueOf(cnt.getOrDefault(item, 0)));
138145
}
139-
res.add(tmp);
146+
ans.add(row);
140147
}
141-
return res;
148+
return ans;
142149
}
143150
}
144151
```
@@ -149,36 +156,31 @@ class Solution {
149156
class Solution {
150157
public:
151158
vector<vector<string>> displayTable(vector<vector<string>>& orders) {
152-
unordered_set<int> tables;
153-
unordered_set<string> foods;
154-
unordered_map<string, int> mp;
155-
for (auto& order : orders) {
156-
int table = stoi(order[1]);
157-
string food = order[2];
158-
tables.insert(table);
159-
foods.insert(food);
160-
++mp[order[1] + "." + food];
159+
map<int, vector<string>> tables;
160+
set<string> sortedItems;
161+
for (auto& o : orders) {
162+
int table = stoi(o[1]);
163+
string foodItem = o[2];
164+
tables[table].push_back(foodItem);
165+
sortedItems.insert(foodItem);
161166
}
162-
vector<int> t;
163-
t.assign(tables.begin(), tables.end());
164-
sort(t.begin(), t.end());
165-
vector<string> f;
166-
f.assign(foods.begin(), foods.end());
167-
sort(f.begin(), f.end());
168-
vector<vector<string>> res;
169-
vector<string> title;
170-
title.push_back("Table");
171-
for (auto e : f) title.push_back(e);
172-
res.push_back(title);
173-
for (int table : t) {
174-
vector<string> tmp;
175-
tmp.push_back(to_string(table));
176-
for (string food : f) {
177-
tmp.push_back(to_string(mp[to_string(table) + "." + food]));
167+
vector<vector<string>> ans;
168+
vector<string> header = {"Table"};
169+
header.insert(header.end(), sortedItems.begin(), sortedItems.end());
170+
ans.push_back(header);
171+
for (auto& [table, items] : tables) {
172+
unordered_map<string, int> cnt;
173+
for (string& item : items) {
174+
cnt[item]++;
175+
}
176+
vector<string> row;
177+
row.push_back(to_string(table));
178+
for (const string& item : sortedItems) {
179+
row.push_back(to_string(cnt[item]));
178180
}
179-
res.push_back(tmp);
181+
ans.push_back(row);
180182
}
181-
return res;
183+
return ans;
182184
}
183185
};
184186
```
@@ -187,43 +189,74 @@ public:
187189
188190
```go
189191
func displayTable(orders [][]string) [][]string {
190-
tables := make(map[int]bool)
191-
foods := make(map[string]bool)
192-
mp := make(map[string]int)
192+
tables := make(map[int]map[string]int)
193+
items := make(map[string]bool)
193194
for _, order := range orders {
194-
table, food := order[1], order[2]
195-
t, _ := strconv.Atoi(table)
196-
tables[t] = true
197-
foods[food] = true
198-
key := table + "." + food
199-
mp[key] += 1
200-
}
201-
var t []int
202-
var f []string
203-
for i := range tables {
204-
t = append(t, i)
195+
table, _ := strconv.Atoi(order[1])
196+
foodItem := order[2]
197+
if tables[table] == nil {
198+
tables[table] = make(map[string]int)
199+
}
200+
tables[table][foodItem]++
201+
items[foodItem] = true
205202
}
206-
for i := range foods {
207-
f = append(f, i)
203+
sortedItems := make([]string, 0, len(items))
204+
for item := range items {
205+
sortedItems = append(sortedItems, item)
208206
}
209-
sort.Ints(t)
210-
sort.Strings(f)
211-
var res [][]string
212-
var title []string
213-
title = append(title, "Table")
214-
for _, e := range f {
215-
title = append(title, e)
207+
sort.Strings(sortedItems)
208+
ans := [][]string{}
209+
header := append([]string{"Table"}, sortedItems...)
210+
ans = append(ans, header)
211+
tableNums := make([]int, 0, len(tables))
212+
for table := range tables {
213+
tableNums = append(tableNums, table)
216214
}
217-
res = append(res, title)
218-
for _, table := range t {
219-
var tmp []string
220-
tmp = append(tmp, strconv.Itoa(table))
221-
for _, food := range f {
222-
tmp = append(tmp, strconv.Itoa(mp[strconv.Itoa(table)+"."+food]))
215+
sort.Ints(tableNums)
216+
for _, table := range tableNums {
217+
row := []string{strconv.Itoa(table)}
218+
for _, item := range sortedItems {
219+
count := tables[table][item]
220+
row = append(row, strconv.Itoa(count))
223221
}
224-
res = append(res, tmp)
222+
ans = append(ans, row)
225223
}
226-
return res
224+
return ans
225+
}
226+
```
227+
228+
#### TypeScript
229+
230+
```ts
231+
function displayTable(orders: string[][]): string[][] {
232+
const tables: Record<number, Record<string, number>> = {};
233+
const items: Set<string> = new Set();
234+
for (const [_, table, foodItem] of orders) {
235+
const t = +table;
236+
if (!tables[t]) {
237+
tables[t] = {};
238+
}
239+
if (!tables[t][foodItem]) {
240+
tables[t][foodItem] = 0;
241+
}
242+
tables[t][foodItem]++;
243+
items.add(foodItem);
244+
}
245+
const sortedItems = Array.from(items).sort();
246+
const ans: string[][] = [];
247+
const header: string[] = ['Table', ...sortedItems];
248+
ans.push(header);
249+
const sortedTableNumbers = Object.keys(tables)
250+
.map(Number)
251+
.sort((a, b) => a - b);
252+
for (const table of sortedTableNumbers) {
253+
const row: string[] = [table.toString()];
254+
for (const item of sortedItems) {
255+
row.push((tables[table][item] || 0).toString());
256+
}
257+
ans.push(row);
258+
}
259+
return ans;
227260
}
228261
```
229262

0 commit comments

Comments
 (0)