Display Table of Food Orders in a Restaurant LeetCode solution in C++

1
class Solution {
2
public:
3
vector<vector<string>> displayTable(vector<vector<string>> &orders) {
4
vector<vector<string>> ans;
5

6
map<int, map<string, int>> m;
7

8
set<string> s; // Sets are useful as they dont contain duplicates as well
9
// arranges the strings in order.
10

11
for (auto row : orders)
12

13
{
14
s.insert(row[2]);
15

16
m[stoi(row[1])][row[2]]++;
17
}
18

19
vector<string> dem;
20

21
dem.push_back("Table");
22

23
for (auto a : s)
24

25
{
26
dem.push_back(a);
27

28
} // For the first row only
29

30
ans.push_back(dem);
31

32
for (auto it : m)
33

34
{
35
vector<string> row;
36

37
row.push_back(to_string(it.first));
38

39
auto dummy = it.second;
40

41
for (auto st : s) // we use set here as it has food names stored in asc order.
42

43
{
44
row.push_back(to_string(dummy[st])); // we access the number of orders.
45
}
46

47
ans.push_back(row);
48
}
49

50
return ans;
51
}
52
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0