6 min read
1297 words
Problem Statement
leetcode problem link
Solution [Accepted]
class Solution:
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
mapping = {}
tables = set()
foods = set()
for customerName, tableNumber, foodItem in orders:
if tableNumber not in mapping:
mapping[tableNumber] = {}
if foodItem not in mapping[tableNumber]:
mapping[tableNumber][foodItem] = 0
mapping[tableNumber][foodItem] += 1
tables.add(tableNumber)
foods.add(foodItem)
sorted_foods = [food for food in sorted(foods)]
foodIndices = {food: i + 1 for i, food in enumerate(sorted_foods)}
sorted_tables = sorted(tables, key=lambda x:int(x))
headers = ["Table"] + sorted_foods
res = [headers]
for table in sorted_tables:
curr = [table]
for _ in range(len(sorted_foods)):
curr.append("0")
arr = []
for food, numOfOrder in mapping[table].items():
index = foodIndices[food]
curr[index] = str(numOfOrder)
res.append(curr[:])
return res
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution
{
public IList<IList<string>> DisplayTable(IList<IList<string>> orders)
{
// Map: tableNumber -> (food -> count)
var tableFood = new Dictionary<int, Dictionary<string, int>>();
var foods = new HashSet<string>();
var tables = new HashSet<int>();
foreach (var order in orders)
{
int table = int.Parse(order[1]); // "12" -> 12
string food = order[2];
if (!tableFood.TryGetValue(table, out var foodMap))
{
foodMap = new Dictionary<string, int>();
tableFood[table] = foodMap;
}
foodMap[food] = foodMap.TryGetValue(food, out var c) ? c + 1 : 1;
tables.Add(table);
foods.Add(food);
}
var sortedFoods = foods.OrderBy(f => f, StringComparer.Ordinal).ToList();
var sortedTables = tables.OrderBy(t => t).ToList();
var result = new List<IList<string>>(sortedTables.Count + 1);
// Header
var header = new List<string>(sortedFoods.Count + 1) { "Table" };
header.AddRange(sortedFoods);
result.Add(header);
// Rows
foreach (var table in sortedTables)
{
var row = new string[sortedFoods.Count + 1];
row[0] = table.ToString();
var foodMap = tableFood[table];
for (int i = 0; i < sortedFoods.Count; i++)
{
string food = sortedFoods[i];
row[i + 1] = foodMap.TryGetValue(food, out int cnt) ? cnt.ToString() : "0";
}
result.Add(row);
}
return result;
}
}
using System;
using System.Collections.Generic;
public class Solution
{
public IList<IList<string>> DisplayTable(IList<IList<string>> orders)
{
var tableFood = new Dictionary<int, Dictionary<string, int>>();
var foods = new SortedSet<string>(StringComparer.Ordinal);
var tables = new SortedSet<int>();
foreach (var order in orders)
{
int table = int.Parse(order[1]);
string food = order[2];
if (!tableFood.TryGetValue(table, out var foodMap))
{
foodMap = new Dictionary<string, int>();
tableFood[table] = foodMap;
}
foodMap[food] = foodMap.TryGetValue(food, out var c) ? c + 1 : 1;
tables.Add(table);
foods.Add(food);
}
var sortedFoods = new List<string>(foods);
var result = new List<IList<string>>(tables.Count + 1);
var header = new List<string>(sortedFoods.Count + 1) { "Table" };
header.AddRange(sortedFoods);
result.Add(header);
foreach (var table in tables)
{
var row = new string[sortedFoods.Count + 1];
row[0] = table.ToString();
var foodMap = tableFood[table];
for (int i = 0; i < sortedFoods.Count; i++)
{
string food = sortedFoods[i];
row[i + 1] = foodMap.TryGetValue(food, out int cnt) ? cnt.ToString() : "0";
}
result.Add(row);
}
return result;
}
}
Leave a comment