LeetCode1418(2021.7.6)

LC1418. 点菜展示表

class Solution {
public:
    vector<vector<string>> displayTable(vector<vector<string>> &orders) {
        set<string> nameSet;
        map<int, unordered_map<string, int>> foodsCnt;
        for (vector<string> order : orders) {
            nameSet.insert(order[2]);
            foodsCnt[stoi(order[1])][order[2]]++;
        }
        
        int n = nameSet.size();
        vector<string> names;
        for (string name : nameSet) names.push_back(name);
        
        int m = foodsCnt.size();
        vector<int> ids;
        for (auto [id, _] : foodsCnt) ids.push_back(id);

        vector<vector<string>> table(m + 1, vector<string>(n + 1));
        table[0][0] = "Table";
        copy(names.begin(), names.end(), table[0].begin() + 1);
        
        for (int i = 0; i < m; ++i) {
            table[i + 1][0] = to_string(ids[i]);
            auto cnt = foodsCnt[ids[i]];
            for (int j = 0; j < n; ++j)
                table[i + 1][j + 1] = to_string(cnt[names[j]]);
        }
        return table;
    }
};

发表评论