Day13力扣打卡
打卡记录
奖励最顶尖的 k 名学生(哈希表+排序)
用哈希表对所有的positive与negative词条进行映射,然后遍历求解。tip:常用的分割字符串的操作:1.stringstream配合getline() [格式buf, string, char]2.string.find()[find未找到目标会返回npos]配合string.substr()
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;
}
};