Problem solution · C++

Display Table of Food Orders in a Restaurant

Display Table of Food Orders in a Restaurant: a C++ solution using sorting and greedy selection. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sorting and greedy selection
Source
walkccc LeetCode Solutions
Length
52 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sorting and greedy selection

For Display Table of Food Orders in a Restaurant, the implementation first exposes a useful order, then scans that order while making locally justified choices.

  1. Choose the key that reveals the greedy or grouping structure.
  2. Sort the relevant records by that key.
  3. Scan in order, maintaining the invariant that makes each local choice safe.

Code notes

  • 52 lines of C++ from the credited upstream file 1418.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 9 loop blocks detected.

Complexity

Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeDisplay Table of Food Orders in a Restaurant · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  vector<vector<string>> displayTable(vector<vector<string>>& orders) {    vector<vector<string>> ans{{"Table"}};    unordered_map<string, int> tableNumberToRowIndex;    unordered_map<string, int> foodItemToColIndex;     // Create the first row and column of ans.    for (const vector<string>& order : orders) {      const string& tableNumber = order[1];      const string& foodItem = order[2];      // Initialize indices with 0s.      tableNumberToRowIndex[tableNumber] = 0;      foodItemToColIndex[foodItem] = 0;    }    for (const auto& [tableNumber, _] : tableNumberToRowIndex)      ans.push_back({tableNumber});    for (const auto& [foodItem, _] : foodItemToColIndex)      ans[0].push_back(foodItem);     // Sort the first row and the column, except ans[0][0].    sort(ans[0].begin() + 1, ans[0].end());    ranges::sort(ans.begin() + 1, ans.end(), ranges::less{},                 [](const vector<string>& cols) { return stoi(cols[0]); });     // Set the indices after sorting.    for (int i = 0; i < tableNumberToRowIndex.size(); ++i)      tableNumberToRowIndex[ans[i + 1][0]] = i;    for (int i = 0; i < foodItemToColIndex.size(); ++i)      foodItemToColIndex[ans[0][i + 1]] = i;     // Get all the counts of each food item in each table.    vector<vector<int>> count;    for (int i = 0; i < tableNumberToRowIndex.size(); ++i)      count.push_back(vector<int>(foodItemToColIndex.size()));    for (const vector<string>& order : orders) {      const string& tableNumber = order[1];      const string& foodItem = order[2];      const int rowIndex = tableNumberToRowIndex[tableNumber];      const int colIndex = foodItemToColIndex[foodItem];      ++count[rowIndex][colIndex];    }     // Set the counts to `ans`.    for (int i = 0; i < tableNumberToRowIndex.size(); ++i)      for (int j = 0; j < foodItemToColIndex.size(); ++j)        ans[i + 1].push_back(to_string(count[i][j]));     return ans;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗