6
6
7
7
<p >Given  ; the array <code >orders</code >, which represents the orders that customers have done in a restaurant. More specifically  ; <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 >  ; is the table customer sit at, and <code >foodItem<sub >i</sub ></code >  ; is the item customer orders.</p >
8
8
9
-
10
-
11
9
<p ><em >Return the restaurant' ; 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 >
12
10
13
-
14
-
15
11
<p >  ; </p >
16
12
17
13
<p ><strong >Example 1:</strong ></p >
18
14
19
-
20
-
21
15
<pre >
22
16
23
17
<strong >Input:</strong > orders = [[" ; David" ; ," ; 3" ; ," ; Ceviche" ; ],[" ; Corina" ; ," ; 10" ; ," ; Beef Burrito" ; ],[" ; David" ; ," ; 3" ; ," ; Fried Chicken" ; ],[" ; Carla" ; ," ; 5" ; ," ; Water" ; ],[" ; Carla" ; ," ; 5" ; ," ; Ceviche" ; ],[" ; Rous" ; ," ; 3" ; ," ; Ceviche" ; ]]
@@ -44,12 +38,8 @@ For the table 10: Corina orders "Beef Burrito".
44
38
45
39
</pre >
46
40
47
-
48
-
49
41
<p ><strong >Example 2:</strong ></p >
50
42
51
-
52
-
53
43
<pre >
54
44
55
45
<strong >Input:</strong > orders = [[" ; James" ; ," ; 12" ; ," ; Fried Chicken" ; ],[" ; Ratesh" ; ," ; 12" ; ," ; Fried Chicken" ; ],[" ; Amadeus" ; ," ; 12" ; ," ; Fried Chicken" ; ],[" ; Adam" ; ," ; 1" ; ," ; Canadian Waffles" ; ],[" ; Brianna" ; ," ; 1" ; ," ; Canadian Waffles" ; ]]
@@ -64,12 +54,8 @@ For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
64
54
65
55
</pre >
66
56
67
-
68
-
69
57
<p ><strong >Example 3:</strong ></p >
70
58
71
-
72
-
73
59
<pre >
74
60
75
61
<strong >Input:</strong > orders = [[" ; Laura" ; ," ; 2" ; ," ; Bean Burrito" ; ],[" ; Jhon" ; ," ; 2" ; ," ; Beef Burrito" ; ],[" ; Melissa" ; ," ; 2" ; ," ; Soda" ; ]]
@@ -78,14 +64,10 @@ For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
78
64
79
65
</pre >
80
66
81
-
82
-
83
67
<p >  ; </p >
84
68
85
69
<p ><strong >Constraints:</strong ></p >
86
70
87
-
88
-
89
71
<ul >
90
72
<li><code>1 <= orders.length <= 5 * 10^4</code></li>
91
73
<li><code>orders[i].length == 3</code></li>
@@ -101,13 +83,149 @@ For the table 12: James, Ratesh and Amadeus order "Fried Chicken".
101
83
### ** Python3**
102
84
103
85
``` 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
105
104
```
106
105
107
106
### ** Java**
108
107
109
108
``` 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
+ ```
110
186
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
+ }
111
229
```
112
230
113
231
### ** ...**
0 commit comments