Day13力扣打卡

打卡记录

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

用哈希表对所有的positive与negative词条进行映射,然后遍历求解。tip:常用的分割字符串的操作:1.stringstream配合getline() 格式buf, string, char2.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;
    }
};
相关推荐
Niuguangshuo4 小时前
论文解读:CTC,端到端序列识别的开山之作
算法·语音识别
讳疾忌医丶4 小时前
深度拆解 RocksDB 内核:基于 C++17 的 FIFO 调度状态机与温度阶梯自愈设计
java·c++·算法·架构
2401_868534785 小时前
OpenStack 架构全景:八个核心组件深度拆解
c++·需求分析
高亦真5 小时前
今天是学习嵌入式的第37天
linux·学习·算法
软件课代表CiCi5 小时前
运行库是什么?电脑缺运行库怎么办?c++运行库缺失修复全攻略
开发语言·c++·windows·电脑·dll修复·dll丢失·运行库
蒸蒸yyyyzwd6 小时前
从面经中学到的三件事
c++·笔记
时时三省6 小时前
【时时三省】计算机c语言二级考试选择题重点
c++
罗西的思考7 小时前
[Agent Memory / 强化学习] MemPO源码学习笔记 --- (2)--- 训练
人工智能·算法·机器学习
深圳市方中禾科技8 小时前
FZH1625 LCD 驱动芯片深度评测与实战指南
算法·led
我不会起名字3228 小时前
一天一道算法题(34):回溯法的经典例题(子集)
java·数据结构·python·算法·golang·深度优先·力扣