Let's make this one less mysterious. For 1418. Display Table of Food Orders in a Restaurant, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are header.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 320 ms, faster than 87.95% of C++ online submissions for Display Table of Food Orders in a Restaurant.
02//Memory Usage: 63.7 MB, less than 100.00% of C++ online submissions for Display Table of Food Orders in a Restaurant.
03class Solution {
04public:
05 vector<vector<string>> displayTable(vector<vector<string>>& orders) {
06 set<string> dishes;
07 map<int, map<string, int>> table2orders;
08
09 for(vector<string>& order : orders){
10 table2orders[stoi(order[1])][order[2]] += 1;
11 dishes.insert(order[2]);
12 }
13
14 vector<vector<string>> ans;
15 vector<string> header(dishes.begin(), dishes.end());
16
17 header.insert(header.begin(), "Table");
18 ans.push_back(header);
19
20 for(auto ait = table2orders.begin(); ait != table2orders.end(); ait++){
21 vector<string> row = {to_string(ait->first)};
22 for(int i = 1; i < header.size(); i++){
23 if(ait->second.find(header[i]) == ait->second.end()){
24 row.push_back("0");
25 }else{
26 row.push_back(to_string(ait->second[header[i]]));
27 }
28
29 }
30 ans.push_back(row);
31 }
32
33 return ans;
34 }
35};
Cost