-
Notifications
You must be signed in to change notification settings - Fork 0
/
2512. Reward Top K Students.cpp
81 lines (63 loc) · 2.17 KB
/
2512. Reward Top K Students.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// https://leetcode.com/problems/reward-top-k-students/
class Solution
{
private:
struct Comparator
{
bool operator()(pair<int, int> &a, pair<int, int> &b)
{
if (a.second == b.second)
return a.first > b.first;
return b.second > a.second;
}
};
public:
vector<int> topStudents(vector<string> &positive_feedback, vector<string> &negative_feedback, vector<string> &report, vector<int> &student_id, int k)
{
vector<int> result;
priority_queue<pair<int, int>, vector<pair<int, int>>, Comparator> values;
unordered_map<int, int> score;
unordered_set<string> positives;
unordered_set<string> negatives;
for (string &word : positive_feedback)
positives.insert(word);
for (string &word : negative_feedback)
negatives.insert(word);
countScore(report, student_id, score, positives, negatives);
for (auto &pair : score)
values.push(pair);
while (k--)
{
pair<int, int> value = values.top();
result.push_back(value.first);
values.pop();
}
return result;
}
void countScore(vector<string> &report, vector<int> &student_id, unordered_map<int, int> &score, unordered_set<string> &positives, unordered_set<string> &negatives)
{
string word = "";
for (int i = 0; i < report.size(); ++i)
{
score[student_id[i]] = 0;
word = "";
for (char &c : report[i])
{
if (c == ' ')
{
if (positives.find(word) != positives.end())
score[student_id[i]] += 3;
else if (negatives.find(word) != negatives.end())
score[student_id[i]] -= 1;
word = "";
}
else
word += c;
}
if (positives.find(word) != positives.end())
score[student_id[i]] += 3;
else if (negatives.find(word) != negatives.end())
score[student_id[i]] -= 1;
}
}
};