LeeCode 1418. Display Table of Food Orders in a Restaurant

[Med] LeetCode 1418. Display Table of Food Orders in a Restaurant

链接: https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/
Medium:

题目描述:
Given the array orders, which represents the orders that customers have done in a restaurant. More specifically orders[i]=[customerNamei,tableNumberi,foodItemi] where customerNamei is the name of the customer, tableNumberi is the table customer sit at, and foodItemi is the item customer orders.
Return the restaurant’s “display table”. The “display table” is a table whose row entries denote how many of each food item each table ordered. The first column is the table number and the remaining columns correspond to each food item in alphabetical order. The first row should be a header whose first column is “Table”, followed by the names of the food items. Note that the customer names are not part of the table. Additionally, the rows should be sorted in numerically increasing order.

给你一个数组 orders,表示客户在餐厅中完成的订单,确切地说, orders[i]=[customerNamei,tableNumberi,foodItemi] ,其中 customerNamei 是客户的姓名,tableNumberi 是客户所在餐桌的桌号,而 foodItemi 是客户点的餐品名称。
请你返回该餐厅的 点菜展示表 。在这张表中,表中第一行为标题,其第一列为餐桌桌号 “Table” ,后面每一列都是按字母顺序排列的餐品名称。接下来每一行中的项则表示每张餐桌订购的相应餐品数量,第一列应当填对应的桌号,后面依次填写下单的餐品数量。
注意:客户姓名不是点菜展示表的一部分。此外,表中的数据行应该按餐桌桌号升序排列。

Example 1:

Input: orders = [[“David”,“3”,“Ceviche”],[“Corina”,“10”,“Beef Burrito”],[“David”,“3”,“Fried Chicken”],[“Carla”,“5”,“Water”],[“Carla”,“5”,“Ceviche”],[“Rous”,“3”,“Ceviche”]]
Output: [[“Table”,“Beef Burrito”,“Ceviche”,“Fried Chicken”,“Water”],[“3”,“0”,“2”,“1”,“0”],[“5”,“0”,“1”,“0”,“1”],[“10”,“1”,“0”,“0”,“0”]]

Explanation:
The displaying table looks like:
Table,Beef Burrito,Ceviche,Fried Chicken,Water
3 ,0 ,2 ,1 ,0,0
5 ,0 ,1 ,0 ,1
10 ,1 ,0 ,0 ,0

我把上面这个例子整理成如下方便观看的形式

TableBeef BurritoCevicheFried ChickenWater
30210
50101
101000

For the table 3: David orders “Ceviche” and “Fried Chicken”, and Rous orders “Ceviche”.
For the table 5: Carla orders “Water” and “Ceviche”.
For the table 10: Corina orders “Beef Burrito”.

Example 2:

Input: orders = [[“James”,“12”,“Fried Chicken”],[“Ratesh”,“12”,“Fried Chicken”],[“Amadeus”,“12”,“Fried Chicken”],[“Adam”,“1”,“Canadian Waffles”],[“Brianna”,“1”,“Canadian Waffles”]]
Output: [[“Table”,“Canadian Waffles”,“Fried Chicken”],[“1”,“2”,“0”],[“12”,“0”,“3”]]
Explanation:
For the table 1: Adam and Brianna order “Canadian Waffles”.
For the table 12: James, Ratesh and Amadeus order “Fried Chicken”.

Example 3:

Input: orders = [[“Laura”,“2”,“Bean Burrito”],[“Jhon”,“2”,“Beef Burrito”],[“Melissa”,“2”,“Soda”]]
Output: [[“Table”,“Bean Burrito”,“Beef Burrito”,“Soda”],[“2”,“1”,“1”,“1”]]

Constraints:

  • 1 <= orders.length <= 5 * 10^4
  • orders[i].length == 3
  • 1 <= customerNamei.length, foodItemi.length <= 20
  • customerNamei and foodItemi consist of lowercase and uppercase English letters and the space character.
  • tableNumberi is a valid integer between 1 and 500.

Tag: HashMap
解题思路
这道题目别无他法,只能够暴力解。我们首先需要一个嵌套的HashMap tableOfFood, 这个tableOfFood的key是tableID, value是一个map, 这个嵌套的map的Key是一种食物,value是这个食物被下单的数量。所以整个tableOfFood就是为了储存每一桌都有神马菜,每一种菜品又有几道一样的。因为不是每一桌都会点所有的菜品,所以我们为了之后统计所有菜品的个数,我们建立一个items的hashset,既记录了所有出现菜品的名称,又不会出现重复。

然后我们建立一个新的arraylist叫firstRow, 然后将刚才遇到的所有菜品都加入到这个firstRow当中作为目录,因为题目要求菜品名字也要排序,所以之后不要忘记sort一下。

接下来为了按顺序输出table Number,我使用了一个TreeMap自动排序了一下。当然,因为题目告诉我们只有500个table, 我们也可以投机取巧遍历1到500的数字,获取tableOfFood 的内容。因为我们已经将菜目进行了排序,我们就按顺序遍历菜品,然后在table对应的hashmap菜单中获取食物的数目,如果该食物不存在,则添加"0"进入菜单。

最后的最后不要忘记将第一个关键字"table"放到我们的第一个数组中,因为之前只需要菜品的名字。然后我们将第一排放到结果list的第一位。

TIME : O(NLOGN)因为需要给餐桌排序, 可以化简为O(N)
SPACE: O(N)
解法:

class Solution {
    public List<List<String>> displayTable(List<List<String>> orders) {
        Set<String> items = new HashSet<>();
        Map<Integer, Map<String, Integer>> tableOfFood = new HashMap<>();
        
        for(List<String> order: orders){
            String customerName = order.get(0), foodItem = order.get(2);
            items.add(foodItem);
            int tableNumber = Integer.valueOf(order.get(1));
            if(!tableOfFood.containsKey(tableNumber)) tableOfFood.put(tableNumber, new HashMap());
            tableOfFood.get(tableNumber).put(foodItem, tableOfFood.get(tableNumber).getOrDefault(foodItem, 0)+1);
        }
        
        List<String> firstRow = new ArrayList<>();
        firstRow.addAll(items);
        Collections.sort(firstRow);
        
        List<List<String>> res = new ArrayList<>();
        Map<Integer, Map<String, Integer>> treeMap = new TreeMap<>(tableOfFood);
        
        for (Map.Entry<Integer, Map<String, Integer>> entry : treeMap.entrySet()) {
            List<String> list = new ArrayList<>();
            Map<String, Integer> table = entry.getValue();
            list.add(String.valueOf(entry.getKey()));
            for(String s: firstRow){
                if(table.containsKey(s)) list.add(String.valueOf(table.get(s)));
                else list.add("0");
            }
            res.add(list);
        }
        firstRow.add(0, "Table");
        res.add(0, firstRow);
        
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值