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多平台发布

相关推荐
leobertlan3 小时前
2025年终总结
前端·后端·程序员
面向Google编程3 小时前
从零学习Kafka:数据存储
后端·kafka
易安说AI4 小时前
Claude Opus 4.6 凌晨发布,我体验了一整晚,说说真实感受。
后端
易安说AI4 小时前
Ralph Loop 让Claude无止尽干活的牛马...
前端·后端
易安说AI4 小时前
用 Claude Code 远程分析生产日志,追踪 Claude Max 账户被封原因
后端
颜酱5 小时前
图结构完全解析:从基础概念到遍历实现
javascript·后端·算法
Coder_Boy_8 小时前
基于SpringAI的在线考试系统-考试系统开发流程案例
java·数据库·人工智能·spring boot·后端
掘金者阿豪9 小时前
关系数据库迁移的“暗礁”:金仓数据库如何规避数据完整性与一致性风险
后端
ServBay9 小时前
一个下午,一台电脑,终结你 90% 的 Symfony 重复劳动
后端·php·symfony
sino爱学习9 小时前
高性能线程池实践:Dubbo EagerThreadPool 设计与应用
java·后端