Reward Top K Students
Here’s an explanation of the problem and a guide to solving it:
Problem
Given positive and negative feedback words and a set of reports with associated student IDs, the task is to calculate the points for each student and return the top k students. Positive feedback words add 3 points, while negative feedback words subtract 1 point.
Solution
The solution can be broken down into several steps:
Create Feedback Sets: Convert positive and negative feedback words into sets for faster lookup.
Initialize Student Points: Create a dictionary or an array to store points for each student, initialized to 0.
Calculate Points: Iterate through the feedback reports and corresponding student IDs. For each report, split the report into words and check if each word is in the positive or negative feedback sets. Update the student’s points accordingly.
Sort Students: Sort the students based on their points in non-increasing order, and if the points are the same, then by their ID in non-decreasing order.
Get Top k Students: Return the top k students after sorting.
Code
|
|
Explanation
positive_set
andnegative_set
are used for fast lookup of positive and negative feedback words.points
dictionary keeps track of the points for each student.sorted_students
contains student IDs sorted by their points in non-increasing order and by their ID in non-decreasing order if points are the same.- Finally, the top k students are returned.
This code will provide the correct solution for the given problem and constraints.