题目是LeetCode第185场周赛的第二题,链接:点菜展示表。具体描述见原题 。
这道题其实是道数据结构的题目,考的就是如何保存信息。观察可以发现每个order
里人名是没用的,有用的只是桌号与菜名,所以可以用set
来保存以避免重复,然后还需要一个Map<桌号, Map<菜名, 数量>>
保存每桌的点菜信息。搞定数据结构之后算法上就没问题了,简单的逻辑处理罢了。假设orders
数为n,桌数为p,菜数为q,则时间复杂度为
O
(
n
+
p
l
o
g
p
+
q
l
o
g
q
+
p
q
)
O(n+plogp+qlogq+pq)
O(n+plogp+qlogq+pq),空间复杂度为
O
(
p
q
)
O(pq)
O(pq)。
JAVA版代码如下:
class Solution {
public List<List<String>> displayTable(List<List<String>> orders) {
Set<Integer> ids = new HashSet<>();
Set<String> foods = new HashSet<>();
Map<Integer, Map<String, Integer>> id2food = new HashMap<>();
for (List<String> order : orders) {
int id = Integer.parseInt(order.get(1));
String food = order.get(2);
ids.add(id);
foods.add(food);
Map<String, Integer> foodMap = id2food.getOrDefault(id, new HashMap<>());
foodMap.put(food, foodMap.getOrDefault(food, 0) + 1);
id2food.put(id, foodMap);
}
List<Integer> sortId = new ArrayList<>(ids);
Collections.sort(sortId);
List<String> sortFood = new ArrayList<>(foods);
Collections.sort(sortFood);
List<List<String>> result = new LinkedList<>();
List<String> first = new LinkedList<>();
first.add("Table");
for (String f : sortFood) {
first.add(f);
}
result.add(first);
for (int si : sortId) {
List<String> item = new LinkedList<>();
item.add(String.valueOf(si));
Map<String, Integer> fm = id2food.get(si);
for (String sf : sortFood) {
item.add(String.valueOf(fm.getOrDefault(sf, 0)));
}
result.add(item);
}
return result;
}
}
提交结果如下:
然后可以用空间换时间如下。
JAVA版代码如下:
class Solution {
public List<List<String>> displayTable(List<List<String>> orders) {
Set<String> foods = new TreeSet<>();
for (List<String> order : orders) {
String food = order.get(2);
foods.add(food);
}
Map<String, Integer> food2idx = new HashMap<>();
int count = 1;
for (String food : foods) {
food2idx.put(food, count++);
}
int[][] id2foodCount = new int[501][foods.size() + 1];
for (List<String> order : orders) {
int id = Integer.parseInt(order.get(1));
int foodId = food2idx.get(order.get(2));
++id2foodCount[id][foodId];
id2foodCount[id][0] = 1;
}
List<List<String>> result = new LinkedList<>();
List<String> first = new LinkedList<>();
first.add("Table");
for (String food : foods) {
first.add(food);
}
result.add(first);
for (int i = 0; i <= 500; ++i) {
if (id2foodCount[i][0] == 1) {
List<String> item = new LinkedList<>();
item.add(String.valueOf(i));
for (String food : foods) {
item.add(String.valueOf(id2foodCount[i][food2idx.get(food)]));
}
result.add(item);
}
}
return result;
}
}
提交结果如下:
Python版代码如下:
class Solution:
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
ids = set()
foods = set()
id2food = {}
for order in orders:
id_table = (int)(order[1])
ids.add(id_table)
foods.add(order[2])
if id_table in id2food:
if order[2] in id2food[id_table]:
id2food[id_table][order[2]] += 1
else:
id2food[id_table][order[2]] = 1
else:
id2food[id_table] = {order[2] : 1}
sortId = sorted(list(ids))
sortFood = sorted(list(foods))
result = [['Table']]
for food in sortFood:
result[0].append(food)
for si in sortId:
item = []
item.append(str(si))
for food in sortFood:
if food in id2food[si]:
item.append(str(id2food[si][food]))
else:
item.append('0')
result.append(item)
return result
提交结果如下: