LeetCode 5389. A la carte display table (hash map)

1. Title

Give you an array of orders, which represents the orders completed by the customer in the restaurant. Specifically, orders[i]=[customerNamei,tableNumberi,foodItemi]where customerNamei is the customer's name, tableNumberi is the table number of the customer's table, and foodItemi is the name of the food ordered by the customer.

Please return to the restaurant's a la carte display table .
In this table, the first row in the table is the title, and the first column is the table number "Table", and each subsequent column is the name of the meal arranged alphabetically.
The items in each subsequent row indicate the corresponding number of meals ordered for each table. The first column should be filled with the corresponding table number, followed by the number of meals ordered in turn.

Note: The customer name is not part of the a la carte display form. In addition, the data rows in the table should be arranged in ascending order of the table number .

示例 1:
输入:orders = [["David","3","Ceviche"],
["Corina","10","Beef Burrito"],
["David","3","Fried Chicken"],
["Carla","5","Water"],
["Carla","5","Ceviche"],
["Rous","3","Ceviche"]]
输出:
[["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],
["3","0","2","1","0"],
["5","0","1","0","1"],
["10","1","0","0","0"]] 
解释:
点菜展示表如下所示:
Table,Beef Burrito,Ceviche,Fried Chicken,Water
3    ,0           ,2      ,1            ,0
5    ,0           ,1      ,0            ,1
10   ,1           ,0      ,0            ,0
对于餐桌 3:David 点了 "Ceviche""Fried Chicken",而 Rous 点了 "Ceviche"
而餐桌 5:Carla 点了 "Water""Ceviche"
餐桌 10:Corina 点了 "Beef Burrito" 

示例 2:
输入:orders = [["James","12","Fried Chicken"],
["Ratesh","12","Fried Chicken"],
["Amadeus","12","Fried Chicken"],
["Adam","1","Canadian Waffles"],
["Brianna","1","Canadian Waffles"]]
输出:
[["Table","Canadian Waffles","Fried Chicken"],
["1","2","0"],
["12","0","3"]] 
解释:
对于餐桌 1:Adam 和 Brianna 都点了 "Canadian Waffles"
而餐桌 12:James, Ratesh 和 Amadeus 都点了 "Fried Chicken"

示例 3:
输入:orders = [["Laura","2","Bean Burrito"],["Jhon","2","Beef Burrito"],["Melissa","2","Soda"]]
输出:[["Table","Bean Burrito","Beef Burrito","Soda"],["2","1","1","1"]]
 
提示:
1 <= orders.length <= 5 * 10^4
orders[i].length == 3
1 <= customerNamei.length, foodItemi.length <= 20
customerNamei 和 foodItemi 由大小写英文字母及空格字符 ' ' 组成。
tableNumberi 是 1500 范围内的整数。

Source: LeetCode
Link: https://leetcode-cn.com/problems/display-table-of-food-orders-in-a-restaurant
Copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

2. Problem solving

class Solution {
public:
    vector<vector<string>> displayTable(vector<vector<string>>& orders) {
        unordered_map<string,map<string,int>> m;//桌号,菜,数量
        vector<vector<string>> food = {{"Table"}};//表头
        vector<string> tableId;//桌号
        for(auto& od : orders)
        {
        	m[od[1]][od[2]]++;
        	tableId.push_back(od[1]);//桌号
        	food[0].push_back(od[2]);//菜品
        }
        sort(tableId.begin(), tableId.end(),[&](auto a, auto b){
            if(a.size()==b.size())
                return a < b;
            return a.size() < b.size();
        });//桌号排序
        tableId.erase(unique(tableId.begin(),tableId.end()), tableId.end());
        //桌号去重
        sort(food[0].begin()+1, food[0].end());
        //菜品排序
        food[0].erase(unique(food[0].begin()+1, food[0].end()), food[0].end());
        //菜品去重
        food.resize(tableId.size()+1);//开辟空间
        for(int i = 1; i < food.size(); ++i)
        {
        	food[i].push_back(tableId[i-1]);//第一列,桌号
        	for(int j = 1; j < food[0].size(); ++j)
        		food[i].push_back(to_string(m[food[i][0]][food[0][j]]));
        		//其余列数量
        }
        return food;
    }
};

984 ms 90.2 MB

Published 884 original articles · Like 2546 · Visits 460,000+

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/105615677