[LeetCode] Display Table of Food Orders in a Restaurant

1418. Display Table of Food Orders in a Restaurant

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.

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
class Solution {
public:
vector<vector<string>> displayTable(vector<vector<string>>& orders) {
map<int, unordered_map<string, int>> mp;
set<string> foods;
for(auto& o : orders) {
string no = o[1], food = o[2];
mp[stoi(no)][food]++;
foods.insert(food);
}
vector<vector<string>> res(mp.size() + 1, vector<string>(foods.size() + 1, "0"));
res[0][0] = "Table";
unordered_map<string, int> fmp;
int pos = 1;
for(auto f : foods) {
fmp[f] = pos;
res[0][pos++] = f;
}
pos = 1;
for(auto& [o, foods] : mp) {
res[pos][0] = to_string(o);
for(auto& [f,cnt] : foods) {
res[pos][fmp[f]] = to_string(cnt);
}
pos++;
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/23/PS/LeetCode/display-table-of-food-orders-in-a-restaurant/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.