leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1418.cpp (980B)


      1 class Solution {
      2   public:
      3     vector<vector<string>> displayTable(vector<vector<string>> &orders) {
      4         map<int, vector<string>> tables;
      5         vector<vector<string>> res;
      6         set<string> foods;
      7 
      8         for (const auto &order : orders) {
      9             tables[stoi(order[1])].push_back(order[2]);
     10             foods.insert(order[2]);
     11         }
     12 
     13         unordered_map<string, int> pos;
     14 
     15         int j = 1;
     16         res.push_back({{"Table"}});
     17         for (auto &food : foods) {
     18             res[0].push_back(food);
     19             pos[food] = j++;
     20         }
     21 
     22         for (auto &[table, foods] : tables) {
     23             vector<int> row(res[0].size(), 0);
     24             vector<string> rows;
     25             rows.reserve(res[0].size());
     26             for (const auto &food : foods)
     27                 row[pos[food]]++;
     28             row[0] = table;
     29             for (int r : row)
     30                 rows.push_back(to_string(r));
     31             res.push_back(rows);
     32         }
     33 
     34         return res;
     35     }
     36 };