zoukankan      html  css  js  c++  java
  • [LeetCode] 1418. Display Table of Food Orders in a Restaurant

    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.

    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
    5    ,0           ,1      ,0            ,1
    10   ,1           ,0      ,0            ,0
    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.

    点菜展示表。

    给你一个数组 orders,表示客户在餐厅中完成的订单,确切地说, orders[i]=[customerNamei,tableNumberi,foodItemi] ,其中 customerNamei 是客户的姓名,tableNumberi 是客户所在餐桌的桌号,而 foodItemi 是客户点的餐品名称。

    请你返回该餐厅的 点菜展示表 。在这张表中,表中第一行为标题,其第一列为餐桌桌号 “Table” ,后面每一列都是按字母顺序排列的餐品名称。接下来每一行中的项则表示每张餐桌订购的相应餐品数量,第一列应当填对应的桌号,后面依次填写下单的餐品数量。

    注意:客户姓名不是点菜展示表的一部分。此外,表中的数据行应该按餐桌桌号升序排列。 

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/display-table-of-food-orders-in-a-restaurant
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    思路不难,但是需要细心的一道题。输入是每个客人的名字,桌号和点的菜;输出是需要展示一张表,表头里需要有Table字样和按字母顺序排列的不同的菜,同时剩下的数据需要按桌号从小到大给出。

    这里我需要一个hashmap记录<桌号,<每个不同的菜,数量>>,一个 treeset 记录全局不同的菜,这样输出的时候就可以做到按字母顺序。

    时间O(n^2)

    空间O(n^2)

    Java实现

     1 class Solution {
     2     public List<List<String>> displayTable(List<List<String>> orders) {
     3         HashMap<Integer, HashMap<String, Integer>> map = new HashMap<>();
     4         TreeSet<String> set = new TreeSet<>();
     5         for (List<String> order : orders) {
     6             int table = Integer.parseInt(order.get(1));
     7             String dish = order.get(2);
     8             map.computeIfAbsent(table, k -> new HashMap<>());
     9             map.get(table).put(dish, map.get(table).getOrDefault(dish, 0) + 1);
    10             set.add(dish);
    11         }
    12 
    13         List<String> cap = new ArrayList<>();
    14         cap.add("Table");
    15         for (String dish : set) {
    16             cap.add(dish);
    17         }
    18         List<List<String>> res = new ArrayList<>();
    19         res.add(cap);
    20         for (int i = 1; i <= 500; i++) {
    21             // 桌号不存在就跳过
    22             if (!map.containsKey(i)) {
    23                 continue;
    24             }
    25             List<String> list = new ArrayList<>();
    26             list.add(String.valueOf(i));
    27             HashMap<String, Integer> ds = map.get(i);
    28             for (int j = 1; j < cap.size(); j++) {
    29                 list.add(String.valueOf(ds.getOrDefault(cap.get(j), 0)));
    30             }
    31             res.add(list);
    32         }
    33         return res;
    34     }
    35 }

    LeetCode 题目总结

  • 相关阅读:
    springboot @Select @Insert @Update @Delete
    列表全选与全反选
    日期控件处理
    MyCat
    eclipse中copy qualified name使用方式
    JPA
    java数组
    Java多线程
    Hadoop采样器实现全排序(报错java.io.EOFException)
    Hadoop全排序
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14975337.html
Copyright © 2011-2022 走看看