Day13力扣打卡

打卡记录

奖励最顶尖的 k 名学生(哈希表+排序)

用哈希表对所有的positive与negative词条进行映射,然后遍历求解。tip:常用的分割字符串的操作:1.stringstream配合getline() [格式buf, string, char]2.string.find()[find未找到目标会返回npos]配合string.substr()

cpp 复制代码
class Solution {
public:
    vector<int> topStudents(vector<string>& positive_feedback, vector<string>& negative_feedback, vector<string>& report, vector<int>& student_id, int k) {
        unordered_set<string> pos, neg;
        for (auto& s : positive_feedback) pos.insert(s);
        for (auto& s : negative_feedback) neg.insert(s);
        vector<pair<int, int>> arr;
        int n = student_id.size();
        for (int i = 0; i < n; ++i) {
            stringstream ss;
            ss << report[i];
            string tmp;
            int res = 0;
            while (getline(ss, tmp, ' ')) {
                if (pos.count(tmp)) res += 3; 
                else if (neg.count(tmp)) res--;
            }
            arr.push_back({-res, student_id[i]});
        }
        sort(arr.begin(), arr.end());
        vector<int> ans(k);
        for (int i = 0; i < k; ++i) ans[i] = arr[i].second;
        return ans;
    }
};
相关推荐
郝学胜-神的一滴6 小时前
Qt 高级开发 009: C++ Lambda 表达式
开发语言·c++·qt·软件构建
石山代码6 小时前
C++ 轻量级日志系统
开发语言·c++
smj2302_796826529 小时前
解决leetcode第3943题递增后的数对数量
数据结构·python·算法·leetcode
炽烈小老头9 小时前
【每天学习一点算法 2026/05/25】矩阵中的最长递增路径
学习·算法·矩阵
王老师青少年编程9 小时前
2026年全国青少年信息素养大赛初赛真题(算法应用主题赛C++初中组初赛真题3:文末附答案和解析)
c++·真题·答案·初赛·2026年·青少年信息素养大赛·初中组
轻颂呀10 小时前
C++11——并发库介绍
开发语言·c++
叁散10 小时前
实验报告:5G 仿真环境与基本链路模拟
算法
从负无穷开始的三次元代码生活11 小时前
算法零碎灵感点分享
算法
梓䈑11 小时前
【算法题攻略】快速排序 和 归并排序
数据结构·c++·排序算法
染指111011 小时前
9.LangChain框架(实现RAG)
数据库·人工智能·算法·机器学习·ai·大模型