Leetcode -1418- Order display table

[Blog link]

The path to learning at 🐔

The nuggets home page

[B].

Give you the orders an array, said clients completed orders in the restaurant, specifically, the orders [I] = [customerNamei tableNumberi, foodIt emi], including customerNamei is the customer's name, TableNumberi is the table number of the customer's table, and foodItemi is the name of the meal the customer ordered. Please return to the menu of the restaurant. In this Table, the first column in the Table is the title, the first column is the Table number "Table", and each column is the name of the food in alphabetical order. The items in the next row represent the number of food ordered for each table. The first column should be filled with the corresponding table number, followed by the number of food ordered. Note: Customer names are not part of the order sheet. In addition, rows of data in the table should be arranged in ascending order by table number. The sample1: Enter: 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"] [Table,Beef Burrito,Ceviche,Fried Chicken,Water] [Table,Beef Burrito,Ceviche,Fried Chicken,Water3    ,0           ,2      ,1            ,0
5    ,0           ,1      ,0            ,1
10   ,1           ,0      ,0            ,0For the table3: David"Ceviche""Fried Chicken"And Rous"Ceviche"The table5: Carla"Water""Ceviche"table10Corina points"Beef Burrito"The sample2: Enter: 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 table1: Both Adam and Brianna have"Canadian Waffles"The table12James, Ratesh and Amadeus have all ordered"Fried Chicken"The sample3: Enter: orders = [["Laura"."2"."Bean Burrito"], ["Jhon"."2"."Beef Burrito"], ["Melis
sa"."2"."Soda"] output: [["Table"."Bean Burrito"."Beef Burrito"."Soda"], ["2"."1"."1"."1"]] tip:1 <= orders.length <= 5 * 10^4 
 orders[i].length == 3 
 1 <= customerNamei.length, foodItemi.length <= 20CustomerNamei and foodItemi contain uppercase and lowercase letters and Spaces' 'Composition. TableNumberi is1500Integers in the range. Related Topics Array hash table string ordered collection sort 👍37 👎 0
Copy the code

[答 案]

Leetcode topic link

[making address]

Code link

[introduction]

Train of thought 1

  • Feel the idea is very simple, more is the use of data structure
public List<List<String>> displayTable(List<List<String>> orders) {
            //set to reset the title bar
            Set<String> tempMenu = new HashSet<>();
            Set<String> tempTable = new HashSet<>();
            List<List<String>> res =  new ArrayList<>();
            for (List<String> list: orders
                 ) {
                tempMenu.add(list.get(2));
                tempTable.add(list.get(1));
            }
            List<String> tempTitle = new ArrayList<>();
            List<String> tempIndex = new ArrayList<>();
            tempTitle.addAll(tempMenu);
            tempIndex.addAll(tempTable);
            Collections.sort(tempTitle);
            Collections.sort(tempIndex, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    returnInteger.parseInt(o1) - Integer.parseInt(o2); }}); List<String> title =new ArrayList<>();
            title.add("Table");
            title.addAll(tempTitle);
            // Record the title index
            Map<String,Integer> menuMap = new HashMap<>();
            for (int i = 0; i < title.size(); i++){ menuMap.put(title.get(i),i); } res.add(title);/ / column ramadhin
            Map<String,Integer> tableMap = new HashMap<>();
            int i = 1;
            for ( String index: tempIndex
            ) {
                List<String> table = new ArrayList<>();
                table.add(index);
                tableMap.put(index,i);
                i++;
                / / initialization
                tempMenu.forEach(str ->{
                    table.add("0");
                });
                res.add(table);
            }
            / / column
            for (List<String> list: orders){
                int tableIndex = tableMap.get(list.get(1));
                int menuName = menuMap.get(list.get(2));
                List<String> cnt = res.get(tableIndex);
                cnt.set(menuName,String.valueOf(Integer.parseInt(cnt.get(menuName)) + 1));
            }
            return res;
        }
Copy the code


Time complexity O ( n l g n + 2 n ) Time complexity O(nlg_{n} + 2n)


// Add test cases
public static void main(String[] args) {
        Solution solution = new DisplayTableOfFoodOrdersInARestaurant().new Solution(a);
        List<String> l1 = new ArrayList<>();
        List<String> l2 = new ArrayList<>();
        List<String> l3 = new ArrayList<>();
        List<String> l4 = new ArrayList<>();
        List<String> l5 = new ArrayList<>();
        List<String> l6 = new ArrayList<>();
        List<List<String>> orders = new ArrayList<>();
        l1.add("David");
        l1.add("3");
        l1.add("Ceviche");
        l2.add("Corina");
        l2.add("10");
        l2.add("David");
        l3.add("Beef Burrito");
        l3.add("3");
        l3.add("Fried Chicken");
        l4.add("Carla");
        l4.add("5");
        l4.add("Water");
        l5.add("Carla");
        l5.add("5");
        l5.add("Ceviche");
        l6.add("Rous");
        l6.add("3");
        l6.add("Ceviche");
        orders.add(l1);
        orders.add(l2);
        orders.add(l3);
        orders.add(l4);
        orders.add(l5);
        orders.add(l6);
        System.out.println(solution.displayTable(orders));

    }	
Copy the code