Solution in Python3. Beats 100% of users with Python3
Intuition\r\n Describe your first thoughts on how to solve this problem. \r\n1. We have the input as an array of arrays, where each item has three information t
# Intuition\r\n<!-- Describe your first thoughts on how to solve this problem. -->\r\n1. We have the input as an array of arrays, where each item has three information to provide about the order : [[Customer_Name, Table_Number, Food_Item]]. \r\n2. The output should be in the form of an array of arrays which represents a table. The first item of the array contains the headings. The subsequent items contains details of the orders placed at each table.\r\n# Approach\r\n<!-- Describe your approach to solving the problem. -->\r\nWe need to build and return the output array. The problem can be broken into three parts:\r\n1. **Building the first item of output array (Heading)**\r\n2. **Building the structure other items of output array (Order Details)**\r\n3. **Updating Information about orders**\r\n\r\n##### **Building the first item of output array (Heading)**\r\nWe need to build a set with the Food Items. We will need to use a set as we do not want to add duplicates while traversing the orders(input) array. \r\nThis set is typecast into a list and then sorted alphabetically.\r\nWe also make a dictionary where items of this sorted array are entered as key with values starting from 1.\r\nWe append this array to our output array as the first element(Headings) after adding "Table" to the front of the the array.\r\n##### **Building the structure other items of output array (Order Details)**\r\nWe build a set of Table Numbers used. This set contains table numbers as a string value. We typecast the set into a list. The items are sorted using the integer value  of items.\r\nWe also make a dictionary where items of this sorted array are entered as key with values starting from 1.\r\nWe now enter these orders into the output array as follows:\r\n- We build an array with "0" as value for length equal to number of food items ordered. Let\'s call this as "Temp".\r\n- We add items to output array as an array where the first item is a table number (taken from the sorted array of table numbers), and followed by the array "Temp" added to it.\r\n\r\n##### **Updating Information about orders**\r\nAt this stage our output array has the required structure. We have the Headings ready and the tables that eventually make orders are loaded with 0 orders as default value.\r\nNow, we traverse the input array(orders). We locate the correct element in our output array using our dictionaries, which provide values from the keys using the food item and table number specified in the input array.\r\nThe selected element in the output array will be a string. It is first typecasted into an integer, incremented, and the again typecasted into a string.\r\nAfter traversing the input array, our output array will be built and can be returned. \r\n\r\n# Complexity\r\n- Time complexity: O(n)\r\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\r\n\r\n<!-- - Space complexity: -->\r\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\r\n\r\n# Code\r\n```\r\nclass Solution:\r\n    def displayTable(self, orders: List[List[str]]) -> List[List[str]]:\r\n        a=set()\r\n        b=set()\r\n        for i in orders:\r\n            a.add(i[2])\r\n            b.add(i[1])\r\n        a=list(a)\r\n        b=list(b)\r\n        a=sorted(a)\r\n        b=sorted(b,key=lambda x:int(x))\r\n        dic_a={}\r\n        dic_b={}\r\n        c=1\r\n        for i in a :\r\n            dic_a[i]=c\r\n            c+=1\r\n        c=1\r\n        for i in b:\r\n            dic_b[i]=c\r\n            c+=1\r\n        output=[]\r\n        output.append(["Table"]+a)\r\n\r\n        crx=["0" for i in range(len(a))]\r\n        for i in b:\r\n            output.append([i]+crx)\r\n        \r\n        for i in orders:\r\n            output[dic_b[i[1]]][dic_a[i[2]]]=int(output[dic_b[i[1]]][dic_a[i[2]]])\r\n            output[dic_b[i[1]]][dic_a[i[2]]]+=1\r\n            output[dic_b[i[1]]][dic_a[i[2]]]=str(output[dic_b[i[1]]][dic_a[i[2]]])\r\n        \r\n        return output\r\n```
['Array', 'Hash Table', 'String', 'Sorting', 'Python', 'Python3']