2512. 奖励最顶尖的 K 名学生

题目

题解

Map + Map

java 复制代码
class Solution {
    public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {

        // 将分数放到 Map 中
        Map<String, Integer> score = new HashMap<>();
        for (String s : positive_feedback) {
            score.put(s, 3);
        }
        for (String s : negative_feedback) {
            score.put(s, -1);
        }

        Map<Integer, Integer> idMap = new HashMap<>();
        for (int i = 0; i < student_id.length; i++) {
            String rep = report[i];
            String[] split = rep.split(" ");
            int cur = 0;
            for (String s : split) {
                cur += score.getOrDefault(s, 0);
            }
            idMap.put(student_id[i], cur);
        }
        // 对 key 进行排序
        return idMap.entrySet().stream().sorted((o1, o2) -> {

                    if (Objects.equals(o1.getValue(), o2.getValue())) {
                        return o1.getKey() - o2.getKey();
                    }
                    return o2.getValue() - o1.getValue();
                }).map(Map.Entry::getKey)
                .limit(k)
                .collect(Collectors.toList());

    }
}

Map + 二维数组

java 复制代码
class Solution {
    public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {

        // 将分数放在 Map
        Map<String, Integer> score = new HashMap<>();
        for (String s : positive_feedback) {
            score.put(s, 3);
        }
        for (String s : negative_feedback) {
            score.put(s, -1);
        }

        int[] scores = new int[student_id.length];
        int[][] idArrs = new int[student_id.length][2];
        for (int i = 0; i < student_id.length; i++) {
            String rep = report[i];
            String[] split = rep.split(" ");
            int cur = 0;
            for (String s : split) {
                cur += score.getOrDefault(s, 0);
            }
            // 分数-学号
            idArrs[i] = new int[]{cur, student_id[i]};
        }
        // 二维数组进行排序
        Arrays.sort(idArrs, (o1, o2) -> {
            if (o1[0] == o2[0]) {
                return o1[1] - o2[1];
            }

            return o2[0] - o1[0];
        });

        ArrayList<Integer> res = new ArrayList<>(k);
        for (int i = 0; i < k; i++) {
            res.add(idArrs[i][1]);
        }
        return res;
    }
}

本文由mdnice多平台发布

相关推荐
吴佳浩40 分钟前
Python入门指南(五) - 为什么选择 FastAPI?
后端·python·fastapi
GoGeekBaird2 小时前
分享几个使用Nano Banana Pro 画信息图的提示词
后端·github
shoubepatien2 小时前
JAVA -- 08
java·后端·intellij-idea
yangminlei2 小时前
springboot pom.xml配置文件详细解析
java·spring boot·后端
黄俊懿2 小时前
【深入理解SpringCloud微服务】Seata(AT模式)源码解析——全局事务的提交
java·后端·spring·spring cloud·微服务·架构·架构师
白宇横流学长2 小时前
基于SpringBoot实现的历史馆藏系统设计与实现【源码+文档】
java·spring boot·后端
moxiaoran57532 小时前
Go语言结构体
开发语言·后端·golang
爱海贼的无处不在3 小时前
现在还有Java面试者不会开发Starter组件
后端·面试·架构
2501_921649493 小时前
免费获取股票历史行情与分时K线数据 API
开发语言·后端·python·金融·数据分析
子洋5 小时前
AI Agent 介绍
前端·人工智能·后端