• 力扣-2512.奖励最顶尖的k名学生


    Idea

    记录caibi学习的第n天,这道题折磨了我两个多小时,终于用自己的思路ac了
    大佬建议参考官方题解思路
    用一个map记录学生id及其得分情况
    用两个set存放 正面词汇集和负面词汇集
    遍历report,然后获取每一个单词,跟两个set进行查找,若正面则map中该id学生得分+3,负面则-1
    因为map中没用根据value排序的函数,我们将得到的map用vector存放,然后自定义vector排序规则
    最后用答案数组取出vector前k个元素就好

    AC Code

    class Solution {
    public:
        vector<int> topStudents(vector<string>& positive_feedback, vector<string>& negative_feedback, vector<string>& report, vector<int>& student_id, int k) {
            int n = student_id.size();
            unordered_map<int, int> student_trans;
            unordered_set<string> posSet;
            unordered_set<string> negSet;
            for(string s : positive_feedback) posSet.insert(s);
            for(string s : negative_feedback) negSet.insert(s);
    
            for(int i = 0; i < n; i++) {
                string tmp = "";
                student_trans[student_id[i]] = 0; // 因为上面的map没有初始化,若该生的得分一直为0的时候,后面将map传递给vector的时候,会出现nullptr的情况,因此这里需要先赋值0
                for(int j = 0; j < report[i].size(); j++) {
                    if(report[i][j] == ' ') {
                        if(posSet.find(tmp) != posSet.end()) student_trans[student_id[i]] +=3;
                        if(negSet.find(tmp) != negSet.end()) student_trans[student_id[i]] -=1;
                        tmp = "";
                        continue;
                    }
                    tmp += report[i][j];
                }
                if(posSet.find(tmp) != posSet.end()) {
                    student_trans[student_id[i]] +=3;
                }
                if(negSet.find(tmp) != negSet.end()) student_trans[student_id[i]] -=1;
            }
            vector<pair<int,int>> student_transVec(student_trans.begin(),student_trans.end());
            sort(student_transVec.begin(),student_transVec.end(),[&] (const auto& l, const auto& r){
                // 如果value相等,比较key值
                if (l.second == r.second)
                    return l.first < r.first;
                else
                // 否则比较value值
                    return  l.second > r.second;
            });
    
            vector<int> ans;
            for(int i = 0; i < k; i++) {
                ans.emplace_back(student_transVec[i].first);
            }
            return ans;
        }
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    在这里插入图片描述

  • 相关阅读:
    flutter系列之:UI layout简介
    x265线程池机制和并行编码机制分析
    mybatis_plus批量处理(开启rewriteBatchedStatements)
    Excel If函数
    elementUI textarea可自适应文本高度的文本域
    FigDraw 17. SCI 文章绘图之主成分绘图(pca3d)
    C语言每日一练——Day01:求最大公约数(三种方法)
    智慧零售解决方案-最新全套文件
    Bean管理注解
    rk3568环境配置和推理报错: RKNN_ERR_MALLOC_FAIL
  • 原文地址:https://blog.csdn.net/weixin_45774972/article/details/133776739