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

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

 

给你两个字符串数组 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. 创建一个哈希表 words,用于存储积极反馈和消极反馈的单词以及它们的分数。

  2. 遍历 positive_feedback 和 negative_feedback,为其中的单词分别赋予分数(积极反馈为3,消极反馈为-1)。

  3. 创建一个二维数组 arr,用于存储每位学生的得分和学生 ID。遍历 report 中的每份报告,将报告中的单词按照 words 中的分数规则计算学生得分,并将得分和学生 ID 存入 arr 中,但为了便于排序,得分取负数。

  4. 对 arr 进行排序,按照得分从高到低排列。

  5. 创建一个 top_k 数组,用于存储前 k 名学生的学生 ID。遍历排序后的 arr,将前 k 名学生的学生 ID 存入 top_k

  6. 返回 top_k,即前 k 名学生的学生 ID 列表。

代码 

class Solution {
public:
    vector<int> topStudents(vector<string>& positive_feedback, vector<string>& negative_feedback, vector<string>& report, vector<int>& student_id, int k) {
        // 创建一个 unordered_map 来存储积极反馈和消极反馈的单词及其对应的分数
        unordered_map<std::string, int> words;
        
        // 遍历积极反馈,为每个单词赋予分数 3
        for(const auto& word : positive_feedback)
            words[word] = 3;
        
        // 遍历消极反馈,为每个单词赋予分数 -1
        for(const auto& word : negative_feedback)
            words[word] = -1;

        // 创建一个二维数组 arr,用于存储每位学生的得分和学生 ID
        vector<vector<int>> arr;

        // 遍历报告
        for (int i = 0; i < report.size(); i++) {
            stringstream ss;
            string word;
            int score = 0;
            
            // 使用 stringstream 分割报告中的单词
            ss << report[i];
            while (ss >> word) {
                // 如果单词在 words 中有分数定义,则将其分数累加到 score 中
                if (words.count(word))
                    score += words[word];
            }
            
            // 将得分和学生 ID 存入 arr 中,但为了便于排序,得分需要取负数
            arr.push_back({-score, student_id[i]});
        }
        
        // 按得分对学生进行排序,得分高的排在前面
        sort(arr.begin(), arr.end());
        
        // 创建一个 vector 来存储前 k 名学生的学生 ID
        vector<int> top_k;
        
        // 遍历前 k 名学生的信息,并将其学生 ID 存入 top_k 中
        for (int i = 0; i < k; i++) {
            top_k.push_back(arr[i][1]);
        }
        
        // 返回前 k 名学生的学生 ID
        return top_k;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值