Easiest Java Solution with Proper Explanation
Intuition \r\nWith the output expected based on table no. sorted and the value\r\nUse TreeMap, With Key as Table Number and Another Map as values which will con
# Intuition \r\nWith the output expected based on table no. sorted and the value\r\nUse TreeMap, With Key as Table Number and Another Map as values which will contain Sorted TreeMap with key of ItemName and Quantity as value\r\n\r\nE.g. **TreeMap<Integer,TreeMap<String,Integer>>**\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n\r\n# Approach\r\nUse TreeMap where\r\nKey would be Table Number\r\nand Values would be another TreeMap where key would be ItemName and Values would be ItemQuantity\r\n<!-- Describe your approach to solving the problem. -->\r\n\r\n# Complexity\r\n- Time complexity:Quadratic (Big O(n*m))\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n- Space complexity:BigO(n*m)\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\nclass Solution {\r\n    public List<List<String>> displayTable(List<List<String>> orders) {         \r\n        //KeyValuePair(map) ItemName as Key and Quantity as Value\r\n        Map<String,Integer> itemNameQuantity; \r\n\r\n        //To get all the unique foodItem\r\n        Set<String>foodItem = new TreeSet(); \r\n\r\n        // Actual Map to contain TableNumber as Key as first output column and the values would be rest output column. \r\n        //Values of the map would be another map with key as ItemName and ItemQuantiy as Values (Refer first line of code)\r\n        TreeMap<Integer,Map<String,Integer>> map = new TreeMap();\r\n\r\n        //Local Variables \r\n        int tableNumber = 0;\r\n        String item="item";\r\n        \r\n        //Input processing from here\r\n        for(List<String> list: orders){\r\n            //Get the table number from first column of each row of input list\r\n            tableNumber = Integer.parseInt(list.get(1));\r\n\r\n            itemNameQuantity = map.get(tableNumber);\r\n            //System.out.println("Temp:"+temp);\r\n            if(itemNameQuantity == null){\r\n                itemNameQuantity = new TreeMap();\r\n                itemNameQuantity.put(list.get(2),1);\r\n                foodItem.add(list.get(2));\r\n            }\r\n            else{\r\n                item = list.get(2);\r\n                foodItem.add(item);\r\n                itemNameQuantity.put(item,itemNameQuantity.containsKey(item)?1+itemNameQuantity.get(item):1);\r\n            }\r\n            map.put(tableNumber,itemNameQuantity);  \r\n        }\r\n\r\n        //Now process from here for the output\r\n        List<List<String>> result = new ArrayList(); //Required output type\r\n        \r\n        //Local List to be added in result list\r\n        List<String> cur = new ArrayList();\r\n\r\n        //Below line is used to add the header in output list\r\n        cur.add("Table");\r\n\r\n        //Now add all the fooditem name\r\n        for(String s:foodItem) cur.add(s);\r\n        result.add(cur);\r\n        \r\n        //Now get all the table items and quantity from the processed actual map\r\n        for(int tableNum:map.keySet()){ //Why, since major map has key as table number and we have tableNumber as first column in the output list \r\n            cur = new ArrayList();\r\n            cur.add(tableNum+""); //Add First Column\r\n\r\n            itemNameQuantity = map.get(tableNum);\r\n            \r\n            //Now process each of the food item name and their quantity for given table number\r\n            for(String s:foodItem){\r\n                cur.add(itemNameQuantity.containsKey(s)?""+itemNameQuantity.get(s):"0");\r\n            }\r\n            //Add the each row into result list\r\n            result.add(cur);\r\n\r\n        }\r\n\r\n        //Return the result list\r\n        return result;\r\n        \r\n    }\r\n}\r\n\r\n\r\n//\uD83D\uDE0A\uD83D\uDE0A Please upvote if you find this helpful \uD83D\uDE0A\uD83D\uDE0A\r\n```
['Hash Table', 'Ordered Map', 'Java']