2023每日刷题(十九)
Leetcode—2512.奖励最顶尖的K名学生
哈希表+stringstream+排序算法思想
实现代码
class Solution {
public:
vector<int> topStudents(vector<string>& positive_feedback, vector<string>& negative_feedback, vector<string>& report, vector<int>& student_id, int k) {
unordered_map<string, int> words;
for(auto pos: positive_feedback) {
words[pos] = 3;
}
for(auto neg: negative_feedback) {
words[neg] = -1;
}
vector<vector<int>> stu;
for(int i = 0; i < report.size(); i++) {
int score = 0;
stringstream ss;
string word;
ss << report[i];
while(ss >> word) {
if(words.count(word)) {
score += words[word];
}
}
stu.push_back({-score, student_id[i]});
}
sort(stu.begin(), stu.end());
vector<int> top;
for(int i = 0; i < k; i++) {
top.push_back(stu[i][1]);
}
return top;
}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!