Skip to content

Commit 304b0aa

Browse files
committed
feat: add solutions to lc problem: No.1418.Display Table of Food Orders in a Restaurant
1 parent bb19e78 commit 304b0aa

File tree

6 files changed

+402
-21
lines changed

6 files changed

+402
-21
lines changed

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

+137-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
<li><code>tableNumber<sub>i</sub></code> 是 <code>1</code> 到 <code>500</code> 范围内的整数。</li>
5757
</ul>
5858

59-
6059
## 解法
6160

6261
<!-- 这里可写通用的实现逻辑 -->
@@ -68,15 +67,151 @@
6867
<!-- 这里可写当前语言的特殊实现逻辑 -->
6968

7069
```python
71-
70+
class Solution:
71+
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
72+
tables = set()
73+
foods = set()
74+
mp = collections.Counter()
75+
for _, table, food in orders:
76+
tables.add(int(table))
77+
foods.add(food)
78+
mp[f'{table}.{food}'] += 1
79+
foods = sorted(list(foods))
80+
tables = sorted(list(tables))
81+
res = [['Table'] + foods]
82+
for table in tables:
83+
t = [str(table)]
84+
for food in foods:
85+
t.append(str(mp[f'{table}.{food}']))
86+
res.append(t)
87+
return res
7288
```
7389

7490
### **Java**
7591

7692
<!-- 这里可写当前语言的特殊实现逻辑 -->
7793

7894
```java
95+
class Solution {
96+
public List<List<String>> displayTable(List<List<String>> orders) {
97+
Set<Integer> tables = new HashSet<>();
98+
Set<String> foods = new HashSet<>();
99+
Map<String, Integer> mp = new HashMap<>();
100+
for (List<String> order : orders) {
101+
int table = Integer.parseInt(order.get(1));
102+
String food = order.get(2);
103+
tables.add(table);
104+
foods.add(food);
105+
String key = table + "." + food;
106+
mp.put(key, mp.getOrDefault(key, 0) + 1);
107+
}
108+
List<Integer> t = new ArrayList<>(tables);
109+
List<String> f = new ArrayList<>(foods);
110+
Collections.sort(t);
111+
Collections.sort(f);
112+
List<List<String>> res = new ArrayList<>();
113+
List<String> title = new ArrayList<>();
114+
title.add("Table");
115+
title.addAll(f);
116+
res.add(title);
117+
for (int table : t) {
118+
List<String> tmp = new ArrayList<>();
119+
tmp.add(String.valueOf(table));
120+
for (String food : f) {
121+
tmp.add(String.valueOf(mp.getOrDefault(table + "." + food, 0)));
122+
}
123+
res.add(tmp);
124+
}
125+
return res;
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
class Solution {
134+
public:
135+
vector<vector<string>> displayTable(vector<vector<string>>& orders) {
136+
unordered_set<int> tables;
137+
unordered_set<string> foods;
138+
unordered_map<string, int> mp;
139+
for (auto& order : orders)
140+
{
141+
int table = stoi(order[1]);
142+
string food = order[2];
143+
tables.insert(table);
144+
foods.insert(food);
145+
++mp[order[1] + "." + food];
146+
}
147+
vector<int> t;
148+
t.assign(tables.begin(), tables.end());
149+
sort(t.begin(), t.end());
150+
vector<string> f;
151+
f.assign(foods.begin(), foods.end());
152+
sort(f.begin(), f.end());
153+
vector<vector<string>> res;
154+
vector<string> title;
155+
title.push_back("Table");
156+
for (auto e : f) title.push_back(e);
157+
res.push_back(title);
158+
for (int table : t)
159+
{
160+
vector<string> tmp;
161+
tmp.push_back(to_string(table));
162+
for (string food : f)
163+
{
164+
tmp.push_back(to_string(mp[to_string(table) + "." + food]));
165+
}
166+
res.push_back(tmp);
167+
}
168+
return res;
169+
}
170+
};
171+
```
79172
173+
### **Go**
174+
175+
```go
176+
func displayTable(orders [][]string) [][]string {
177+
tables := make(map[int]bool)
178+
foods := make(map[string]bool)
179+
mp := make(map[string]int)
180+
for _, order := range orders {
181+
table, food := order[1], order[2]
182+
t, _ := strconv.Atoi(table)
183+
tables[t] = true
184+
foods[food] = true
185+
key := table + "." + food
186+
mp[key] += 1
187+
}
188+
var t []int
189+
var f []string
190+
for i := range tables {
191+
t = append(t, i)
192+
}
193+
for i := range foods {
194+
f = append(f, i)
195+
}
196+
sort.Ints(t)
197+
sort.Strings(f)
198+
var res [][]string
199+
var title []string
200+
title = append(title, "Table")
201+
for _, e := range f {
202+
title = append(title, e)
203+
}
204+
res = append(res, title)
205+
for _, table := range t {
206+
var tmp []string
207+
tmp = append(tmp, strconv.Itoa(table))
208+
for _, food := range f {
209+
tmp = append(tmp, strconv.Itoa(mp[strconv.Itoa(table)+"."+food]))
210+
}
211+
res = append(res, tmp)
212+
}
213+
return res
214+
}
80215
```
81216

82217
### **...**

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

+137-19
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66

77
<p>Given&nbsp;the array <code>orders</code>, which represents the orders that customers have done in a restaurant. More specifically&nbsp;<code>orders[i]=[customerName<sub>i</sub>,tableNumber<sub>i</sub>,foodItem<sub>i</sub>]</code> where <code>customerName<sub>i</sub></code> is the name of the customer, <code>tableNumber<sub>i</sub></code>&nbsp;is the table customer sit at, and <code>foodItem<sub>i</sub></code>&nbsp;is the item customer orders.</p>
88

9-
10-
119
<p><em>Return the restaurant&#39;s &ldquo;<strong>display table</strong>&rdquo;</em>. The &ldquo;<strong>display table</strong>&rdquo; 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 &ldquo;Table&rdquo;, 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.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

1713
<p><strong>Example 1:</strong></p>
1814

19-
20-
2115
<pre>
2216

2317
<strong>Input:</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;]]
@@ -44,12 +38,8 @@ For the table 10: Corina orders &quot;Beef Burrito&quot;.
4438

4539
</pre>
4640

47-
48-
4941
<p><strong>Example 2:</strong></p>
5042

51-
52-
5343
<pre>
5444

5545
<strong>Input:</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;]]
@@ -64,12 +54,8 @@ For the table 12: James, Ratesh and Amadeus order &quot;Fried Chicken&quot;.
6454

6555
</pre>
6656

67-
68-
6957
<p><strong>Example 3:</strong></p>
7058

71-
72-
7359
<pre>
7460

7561
<strong>Input:</strong> orders = [[&quot;Laura&quot;,&quot;2&quot;,&quot;Bean Burrito&quot;],[&quot;Jhon&quot;,&quot;2&quot;,&quot;Beef Burrito&quot;],[&quot;Melissa&quot;,&quot;2&quot;,&quot;Soda&quot;]]
@@ -78,14 +64,10 @@ For the table 12: James, Ratesh and Amadeus order &quot;Fried Chicken&quot;.
7864

7965
</pre>
8066

81-
82-
8367
<p>&nbsp;</p>
8468

8569
<p><strong>Constraints:</strong></p>
8670

87-
88-
8971
<ul>
9072
<li><code>1 &lt;=&nbsp;orders.length &lt;= 5 * 10^4</code></li>
9173
<li><code>orders[i].length == 3</code></li>
@@ -101,13 +83,149 @@ For the table 12: James, Ratesh and Amadeus order &quot;Fried Chicken&quot;.
10183
### **Python3**
10284

10385
```python
104-
86+
class Solution:
87+
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
88+
tables = set()
89+
foods = set()
90+
mp = collections.Counter()
91+
for _, table, food in orders:
92+
tables.add(int(table))
93+
foods.add(food)
94+
mp[f'{table}.{food}'] += 1
95+
foods = sorted(list(foods))
96+
tables = sorted(list(tables))
97+
res = [['Table'] + foods]
98+
for table in tables:
99+
t = [str(table)]
100+
for food in foods:
101+
t.append(str(mp[f'{table}.{food}']))
102+
res.append(t)
103+
return res
105104
```
106105

107106
### **Java**
108107

109108
```java
109+
class Solution {
110+
public List<List<String>> displayTable(List<List<String>> orders) {
111+
Set<Integer> tables = new HashSet<>();
112+
Set<String> foods = new HashSet<>();
113+
Map<String, Integer> mp = new HashMap<>();
114+
for (List<String> order : orders) {
115+
int table = Integer.parseInt(order.get(1));
116+
String food = order.get(2);
117+
tables.add(table);
118+
foods.add(food);
119+
String key = table + "." + food;
120+
mp.put(key, mp.getOrDefault(key, 0) + 1);
121+
}
122+
List<Integer> t = new ArrayList<>(tables);
123+
List<String> f = new ArrayList<>(foods);
124+
Collections.sort(t);
125+
Collections.sort(f);
126+
List<List<String>> res = new ArrayList<>();
127+
List<String> title = new ArrayList<>();
128+
title.add("Table");
129+
title.addAll(f);
130+
res.add(title);
131+
for (int table : t) {
132+
List<String> tmp = new ArrayList<>();
133+
tmp.add(String.valueOf(table));
134+
for (String food : f) {
135+
tmp.add(String.valueOf(mp.getOrDefault(table + "." + food, 0)));
136+
}
137+
res.add(tmp);
138+
}
139+
return res;
140+
}
141+
}
142+
```
143+
144+
### **C++**
145+
146+
```cpp
147+
class Solution {
148+
public:
149+
vector<vector<string>> displayTable(vector<vector<string>>& orders) {
150+
unordered_set<int> tables;
151+
unordered_set<string> foods;
152+
unordered_map<string, int> mp;
153+
for (auto& order : orders)
154+
{
155+
int table = stoi(order[1]);
156+
string food = order[2];
157+
tables.insert(table);
158+
foods.insert(food);
159+
++mp[order[1] + "." + food];
160+
}
161+
vector<int> t;
162+
t.assign(tables.begin(), tables.end());
163+
sort(t.begin(), t.end());
164+
vector<string> f;
165+
f.assign(foods.begin(), foods.end());
166+
sort(f.begin(), f.end());
167+
vector<vector<string>> res;
168+
vector<string> title;
169+
title.push_back("Table");
170+
for (auto e : f) title.push_back(e);
171+
res.push_back(title);
172+
for (int table : t)
173+
{
174+
vector<string> tmp;
175+
tmp.push_back(to_string(table));
176+
for (string food : f)
177+
{
178+
tmp.push_back(to_string(mp[to_string(table) + "." + food]));
179+
}
180+
res.push_back(tmp);
181+
}
182+
return res;
183+
}
184+
};
185+
```
110186
187+
### **Go**
188+
189+
```go
190+
func displayTable(orders [][]string) [][]string {
191+
tables := make(map[int]bool)
192+
foods := make(map[string]bool)
193+
mp := make(map[string]int)
194+
for _, order := range orders {
195+
table, food := order[1], order[2]
196+
t, _ := strconv.Atoi(table)
197+
tables[t] = true
198+
foods[food] = true
199+
key := table + "." + food
200+
mp[key] += 1
201+
}
202+
var t []int
203+
var f []string
204+
for i := range tables {
205+
t = append(t, i)
206+
}
207+
for i := range foods {
208+
f = append(f, i)
209+
}
210+
sort.Ints(t)
211+
sort.Strings(f)
212+
var res [][]string
213+
var title []string
214+
title = append(title, "Table")
215+
for _, e := range f {
216+
title = append(title, e)
217+
}
218+
res = append(res, title)
219+
for _, table := range t {
220+
var tmp []string
221+
tmp = append(tmp, strconv.Itoa(table))
222+
for _, food := range f {
223+
tmp = append(tmp, strconv.Itoa(mp[strconv.Itoa(table)+"."+food]))
224+
}
225+
res = append(res, tmp)
226+
}
227+
return res
228+
}
111229
```
112230

113231
### **...**

0 commit comments

Comments
 (0)