-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path2512-reward-top-k-students.js
More file actions
56 lines (50 loc) · 1.85 KB
/
2512-reward-top-k-students.js
File metadata and controls
56 lines (50 loc) · 1.85 KB
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
/**
* 2512. Reward Top K Students
* https://leetcode.com/problems/reward-top-k-students/
* Difficulty: Medium
*
* You are given two string arrays positive_feedback and negative_feedback, containing the words
* denoting positive and negative feedback, respectively. Note that no word is both positive and
* negative.
*
* Initially every student has 0 points. Each positive word in a feedback report increases the
* points of a student by 3, whereas each negative word decreases the points by 1.
*
* You are given n feedback reports, represented by a 0-indexed string array report and a
* 0-indexed integer array student_id, where student_id[i] represents the ID of the student
* who has received the feedback report report[i]. The ID of each student is unique.
*
* Given an integer k, return the top k students after ranking them in non-increasing order
* by their points. In case more than one student has the same points, the one with the lower
* ID ranks higher.
*/
/**
* @param {string[]} positive_feedback
* @param {string[]} negative_feedback
* @param {string[]} report
* @param {number[]} student_id
* @param {number} k
* @return {number[]}
*/
var topStudents = function(positive_feedback, negative_feedback, report, student_id, k) {
const positiveSet = new Set(positive_feedback);
const negativeSet = new Set(negative_feedback);
const studentScores = [];
for (let i = 0; i < report.length; i++) {
const words = report[i].split(' ');
let score = 0;
for (const word of words) {
if (positiveSet.has(word)) {
score += 3;
} else if (negativeSet.has(word)) {
score -= 1;
}
}
studentScores.push([student_id[i], score]);
}
studentScores.sort((a, b) => {
if (a[1] !== b[1]) return b[1] - a[1];
return a[0] - b[0];
});
return studentScores.slice(0, k).map(student => student[0]);
};