LeetCode 1418. Display Table of Food Orders in a Restaurant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<vector<string>> displayTable(vector<vector<string>>& orders) {
vector<unordered_map<string, int>> tables(501);
set<string> foods;
for (auto &v : orders) {
foods.insert(v[2]);
++tables[stoi(v[1])][v[2]];
}
vector<vector<string>> ans;
for (int t = 0; t <= 500; t++) {
if (t > 0 && tables[t].empty()) continue;
ans.push_back({});
ans.back().emplace_back(t == 0 ? "Table" : to_string(t));
for (auto it = foods.begin(); it != foods.end(); it++)
ans.back().emplace_back(t == 0 ? *it : to_string(tables[t][*it]));
}
return ans;
}
};