Simple Solution - O(N * (log T + M) + T * M)
IntuitionThere isn't much to think about here. It's a matter of getting a proper representation of the tables,meals and orders as described.The real challenge i
# Intuition
There isn't much to think about here. It's a matter of getting a proper representation of the tables,meals and orders as described.

The real challenge in this challenge is not figuiring out the meals ordered but rather putting them together in the exact format requested by the challenge since you have to sort and organize the data correctly.

# Approach
By using a TreeMap to keep track of tables and orders and setting the comparator in advance to compare based on the table number in ascending order, will eventually save you a lot of headache and it's much cleaner since at the end you will get the result you desired.

1- Iterate through the orders to add to a TreeSet all the distinict meal names for the table.
2- Create a HashMap mapping each sorted meal name to the static index it will use in the final answer since all meals in the table will use a static column based on their lexigraphical order.
3- Now you iterate through each order, if the TreeMap doesn't contain the tableNumber, it means that this is the first order, so we populate it with a an ArrayList<String> and fill it with zeroes.
4- Now we can rest assured that our TreeMap won't through exceptions such as NPE. Next, we convert the quantity of meals just to increase it's corresponding integer value and then we set it again in the list and put it back as a String in our TreeMap.
5- Finally, having setup the table's headers already before we started looping through the orders, next, we loop through the final TreeMap values and add the value of each entry (the List of orders tables and number of meals ordered sorted).

At the final step, we won't have to worry about sorting since our TreeMap has already sorted for us the orders by table number and our meals' quantities have been set at the correct positions since we constructed "mealIndex" HashMap to keep track of the proper indexes of each meal name.

Unfortunately, if it wasn't for the strict final table format, we could have optimized this code or reduced time complexity but this is the best you could get.

# Complexity
- Time complexity:
O(N * (log T + M) + T * M)

- Space complexity:
O(N + M + T * (M + 1))

# Code
```java []
class Solution {
    public List<List<String>> displayTable(List<List<String>> orders) {
        final List<List<String>> ans = new ArrayList<>(orders.size());

                Comparator<String> byFirstElementComparator = (s1, s2) -> {

            return Integer.compare(Integer.valueOf(s1),Integer.valueOf(s2)); // Compare the first String element
        };

        TreeMap<String, List<String>> map = new TreeMap<>(byFirstElementComparator);

        Set<String> all = new TreeSet<>();

        for (List<String> li : orders) {
            all.add(li.get(2));
        }

        Map<String, Integer> mealIndex = new HashMap<>(all.size());

        int ind = 1;
        for (String name : all) {
            mealIndex.put(name, ind++);
        }

        List<String> row = new ArrayList<>(all.size() + 1);

        row.add("Table");
        row.addAll(all);

        ans.add(row);

        for (int i = 0; i < orders.size(); i++) {
            String customerName = orders.get(i).get(0);
            String tableNumber = orders.get(i).get(1);
            String meal = orders.get(i).get(2);

            if (!map.containsKey(tableNumber)) {
                List<String> li = new ArrayList<>(all.size() + 1);

                li.add(tableNumber);

                for (int u = 0; u < all.size(); u++) {
                    li.add("0");
                }

                map.put(tableNumber, li);
            }

            int index = mealIndex.get(meal);

            int val = Integer.valueOf(map.get(tableNumber).get(index));

            val++;

            map.get(tableNumber).set(index, String.valueOf(val));

            map.put(tableNumber, map.get(tableNumber));
        }

        map.forEach((k,v) -> {
            ans.add(v);
        });

        return ans;
    }
}
```
['Java']