Moment For Technology

Leetcode One of the day series - a la carte presentation table

Posted on Oct. 3, 2022, 3:57 a.m. by 程詩涵
Category: The back-end Tag: The back-end algorithm

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 is1 到 500Integers 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 ListListString displayTable(ListListString orders) {
            //set to reset the title bar
            SetString tempMenu = new HashSet();
            SetString tempTable = new HashSet();
            ListListString res =  new ArrayList();
            for (ListString list: orders
                 ) {
                tempMenu.add(list.get(2));
                tempTable.add(list.get(1));
            }
            ListString tempTitle = new ArrayList();
            ListString tempIndex = new ArrayList();
            tempTitle.addAll(tempMenu);
            tempIndex.addAll(tempTable);
            Collections.sort(tempTitle);
            Collections.sort(tempIndex, new ComparatorString() {
                @Override
                public int compare(String o1, String o2) {
                    returnInteger.parseInt(o1) - Integer.parseInt(o2); }}); ListString title =new ArrayList();
            title.add("Table");
            title.addAll(tempTitle);
            // Record the title index
            MapString,Integer menuMap = new HashMap();
            for (int i = 0; i  title.size(); i++){ menuMap.put(title.get(i),i); } res.add(title);/ / column ramadhin
            MapString,Integer tableMap = new HashMap();
            int i = 1;
            for ( String index: tempIndex
            ) {
                ListString table = new ArrayList();
                table.add(index);
                tableMap.put(index,i);
                i++;
                / / initialization
                tempMenu.forEach(str -{
                    table.add("0");
                });
                res.add(table);
            }
            / / column
            for (ListString list: orders){
                int tableIndex = tableMap.get(list.get(1));
                int menuName = menuMap.get(list.get(2));
                ListString 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);
        ListString l1 = new ArrayList();
        ListString l2 = new ArrayList();
        ListString l3 = new ArrayList();
        ListString l4 = new ArrayList();
        ListString l5 = new ArrayList();
        ListString l6 = new ArrayList();
        ListListString 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
Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.