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

相关推荐
程序员Leo28 分钟前
OpenClaw 配置指南:DeepSeek 与 飞书集成
后端·agent
彭于晏Yan1 小时前
Springboot实现微服务监控
spring boot·后端·微服务
小江的记录本1 小时前
【Spring Boot—— .yml(YAML)】Spring Boot中.yml文件的基础语法、高级特性、实践技巧
xml·java·spring boot·后端·spring·spring cloud·架构
爱敲代码的小黄1 小时前
Agent 能力模块化:Skill 设计与执行机制解析
人工智能·后端·面试
掘金者阿豪2 小时前
告别SQL性能焦虑:金仓数据库“连接条件下推”的性能魔法
后端
老友@2 小时前
接口调用的演进史——从“发 HTTP 请求”到“可治理的系统能力
spring boot·后端·架构
zzb15802 小时前
RAG from Scratch-优化-routing
java·前端·网络·人工智能·后端·python·mybatis
酱紫学Java2 小时前
数据安全比赛:Python 内置函数实战指南
后端·python·网络安全
程途知微3 小时前
Java 内存模型 (JMM) 与 volatile 底层实现
java·后端
手握风云-3 小时前
Spring AI:让大模型住进 Spring 生态(二)
java·后端·spring