leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1418.cpp (980B)
0 class Solution { 1 public: 2 vector<vector<string>> displayTable(vector<vector<string>> &orders) { 3 map<int, vector<string>> tables; 4 vector<vector<string>> res; 5 set<string> foods; 6 7 for (const auto &order : orders) { 8 tables[stoi(order[1])].push_back(order[2]); 9 foods.insert(order[2]); 10 } 11 12 unordered_map<string, int> pos; 13 14 int j = 1; 15 res.push_back({{"Table"}}); 16 for (auto &food : foods) { 17 res[0].push_back(food); 18 pos[food] = j++; 19 } 20 21 for (auto &[table, foods] : tables) { 22 vector<int> row(res[0].size(), 0); 23 vector<string> rows; 24 rows.reserve(res[0].size()); 25 for (const auto &food : foods) 26 row[pos[food]]++; 27 row[0] = table; 28 for (int r : row) 29 rows.push_back(to_string(r)); 30 res.push_back(rows); 31 } 32 33 return res; 34 } 35 };