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;
    }
};
相关推荐
John_ToDebug7 小时前
浏览器扩展延迟加载优化实战:如何让浏览器启动速度提升50%
c++·chrome·windows
是娇娇公主~7 小时前
C++ 中 std::deque 的原理?它内部是如何实现的?
开发语言·c++·stl
Fly Wine8 小时前
Leetcode之有效字母异位词
算法·leetcode·职场和发展
程序员夏末9 小时前
【LeetCode | 第七篇】算法笔记
笔记·算法·leetcode
luanma15098010 小时前
PHP vs C++:编程语言终极对决
开发语言·c++·php
csdn_aspnet10 小时前
C/C++ 两个凸多边形之间的切线(Tangents between two Convex Polygons)
c语言·c++·算法
数据皮皮侠10 小时前
中国城市间地理距离矩阵(2024)
大数据·数据库·人工智能·算法·制造
3GPP仿真实验室10 小时前
深度解析基站接收机核心算法:从 MRC 到 IRC 的空间滤波演进
算法
Boop_wu10 小时前
[Java 算法] 动态规划(1)
算法·动态规划
WolfGang00732110 小时前
代码随想录算法训练营 Day18 | 二叉树 part08
算法