leetcode-点菜展示表

 题目是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

 提交结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值