• 【LeetCode:2512. 奖励最顶尖的 K 名学生 | 模拟+哈希表+堆】


    在这里插入图片描述

    🚀 算法题 🚀

    🌲 算法刷题专栏 | 面试必备算法 | 面试高频算法 🍀
    🌲 越难的东西,越要努力坚持,因为它具有很高的价值,算法就是这样✨
    🌲 作者简介:硕风和炜,CSDN-Java领域新星创作者🏆,保研|国家奖学金|高中学习JAVA|大学完善JAVA开发技术栈|面试刷题|面经八股文|经验分享|好用的网站工具分享💎💎💎
    🌲 恭喜你发现一枚宝藏博主,赶快收入囊中吧🌻
    🌲 人生如棋,我愿为卒,行动虽慢,可谁曾见我后退一步?🎯🎯

    🚀 算法题 🚀

    在这里插入图片描述

    在这里插入图片描述

    🚩 题目链接

    ⛲ 题目描述

    给你两个字符串数组 positive_feedback 和 negative_feedback ,分别包含表示正面的和负面的词汇。不会 有单词同时是正面的和负面的。

    一开始,每位学生分数为 0 。每个正面的单词会给学生的分数 加 3 分,每个负面的词会给学生的分数 减 1 分。

    给你 n 个学生的评语,用一个下标从 0 开始的字符串数组 report 和一个下标从 0 开始的整数数组 student_id 表示,其中 student_id[i] 表示这名学生的 ID ,这名学生的评语是 report[i] 。每名学生的 ID 互不相同。

    给你一个整数 k ,请你返回按照得分 从高到低 最顶尖的 k 名学生。如果有多名学生分数相同,ID 越小排名越前。

    示例 1:

    输入:positive_feedback = [“smart”,“brilliant”,“studious”], negative_feedback = [“not”], report = [“this student is studious”,“the student is smart”], student_id = [1,2], k = 2
    输出:[1,2]
    解释:
    两名学生都有 1 个正面词汇,都得到 3 分,学生 1 的 ID 更小所以排名更前。
    示例 2:

    输入:positive_feedback = [“smart”,“brilliant”,“studious”], negative_feedback = [“not”], report = [“this student is not studious”,“the student is smart”], student_id = [1,2], k = 2
    输出:[2,1]
    解释:

    • ID 为 1 的学生有 1 个正面词汇和 1 个负面词汇,所以得分为 3-1=2 分。
    • ID 为 2 的学生有 1 个正面词汇,得分为 3 分。
      学生 2 分数更高,所以返回 [2,1] 。

    提示:

    1 <= positive_feedback.length, negative_feedback.length <= 104
    1 <= positive_feedback[i].length, negative_feedback[j].length <= 100
    positive_feedback[i] 和 negative_feedback[j] 都只包含小写英文字母。
    positive_feedback 和 negative_feedback 中不会有相同单词。
    n == report.length == student_id.length
    1 <= n <= 104
    report[i] 只包含小写英文字母和空格 ’ ’ 。
    report[i] 中连续单词之间有单个空格隔开。
    1 <= report[i].length <= 100
    1 <= student_id[i] <= 109
    student_id[i] 的值 互不相同 。
    1 <= k <= n

    🌟 求解思路&实现代码&运行结果


    ⚡ 模拟+哈希表+堆

    🥦 求解思路
    1. 该题直接按照题目的要求进行模拟即可,求解思路较为简单。
    2. 特别说明:代码实现方式有很多,先实现,再优划。
    3. 具体求解的过程步骤请看下面代码。
    🥦 实现代码
    class Solution {
        public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
            HashMap<String,String> post=new HashMap<>();
            HashMap<String,String> nega=new HashMap<>();
            for(String s:positive_feedback) post.put(s,s);
            for(String s:negative_feedback) nega.put(s,s);
            int n=student_id.length;
            List<Integer> res=new ArrayList<>();
            PriorityQueue<int[]> queue=new PriorityQueue<>((a,b)->(a[1]!=b[1]?b[1]-a[1]:a[0]-b[0]));
            for(int i=0;i<n;i++){
                String[] str=report[i].split(" ");
                int socre=0;
                for(String st:str){
                    if(post.containsKey(st)) socre+=3;
                    if(nega.containsKey(st)) socre-=1;
                }
                queue.add(new int[]{student_id[i],socre});    
            }
            while(k-->0){
                res.add(queue.poll()[0]);
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    🥦 运行结果

    在这里插入图片描述


    💬 共勉

    最后,我想和大家分享一句一直激励我的座右铭,希望可以与大家共勉!

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    面向服务的软件架构(SOA)
    KT142C语音芯片音乐前要空白音才行,声音会被截掉,实际语音是你好,播放变成好
    Google Cloud Natural Language情感分析教程
    径向基函数拟合
    个人玩航拍,如何申请无人机空域?
    C/C++--跨平台
    【Arduino24】8*8点阵实验
    carsim 2020 安装说明及多个版本冲突问题解决
    Postman连接MySQL数据库并操作
    自学WEB后端01-安装Express+Node.js框架完成Hello World!
  • 原文地址:https://blog.csdn.net/Coder_ljw/article/details/133767175