Python. Faster than 100 % solutions with simple Dict
Complexity\r\n- Time complexity:\r\n1. Linear time to build dict - O(n)\r\n2. Sorting i items by name - O(i log i)\r\n3. Sort Tables + iterate over each dict ti
# Complexity\r\n- Time complexity:\r\n1. Linear time to build dict - O(n)\r\n2. Sorting i items by name - O(i log i)\r\n3. Sort Tables + iterate over each dict time - O (t log t)\r\n\r\nTotal complexity - O(n log n)\r\n \r\n- Space complexity:\r\nStore unique items - O(n)\r\nStore row_item_map - O(n)\r\n\r\nTotal Complexity - O(2n) -> O(n)\r\n\r\n# Code\r\n```\r\nclass Solution(object):\r\n    def displayTable(self, orders):\r\n        """\r\n        :type orders: List[List[str]]\r\n        :rtype: List[List[str]]\r\n        """\r\n        row_item_map = collections.defaultdict(dict)\r\n        items = set()\r\n        res = []\r\n\r\n        for customer, table, item in orders:\r\n            if item in row_item_map[table].keys():\r\n                row_item_map[table][item] += 1\r\n            else:\r\n                row_item_map[table][item] = 1\r\n            items.add(item)\r\n\r\n        res.append(sorted(items))\r\n        for key in sorted(row_item_map.keys(), key=lambda x: int(x)):\r\n            dict_row = row_item_map[key]\r\n            row = [str(key)]\r\n            for item in res[0]:\r\n                if item in dict_row:\r\n                    row.append(str(dict_row[item]))\r\n                else:\r\n                    row.append("0")\r\n            res.append(row)\r\n        res[0] = ["Table"] + res[0]\r\n        return res\r\n\r\n            \r\n        \r\n```
['Python']