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;
    }
};
相关推荐
Zephyrtoria2 小时前
区间合并:区间合并问题
java·开发语言·数据结构·算法
柏箱4 小时前
容器里有10升油,现在只有两个分别能装3升和7升油的瓶子,需要将10 升油等分成2 个5 升油。程序输出分油次数最少的详细操作过程。
算法·bfs
Aderversa4 小时前
C++开源协程库async_simple有栈协程源码分析
c++·协程
uyeonashi4 小时前
【QT】窗口详解
开发语言·c++·qt·学习
Hello eveybody5 小时前
C++介绍整数二分与实数二分
开发语言·数据结构·c++·算法
空白木各6 小时前
evo工具
c++
编码小笨猪6 小时前
浅谈Linux中一次系统调用的执行过程
linux·服务器·c++
Mallow Flowers7 小时前
Python训练营-Day31-文件的拆分和使用
开发语言·人工智能·python·算法·机器学习
GalaxyPokemon8 小时前
LeetCode - 704. 二分查找
数据结构·算法·leetcode
leo__5208 小时前
matlab实现非线性Granger因果检验
人工智能·算法·matlab