Problem Description
We are given two arrays: positive_feedback and negative_feedback, which list the words that contribute positively and negatively (respectively) to a student's feedback score. Each student initially has 0 points, with each occurrence of a positive word in their feedback adding 3 points and each occurrence of a negative word subtracting 1 point. The input also includes a list of feedback reports and their corresponding unique student IDs. The task is to calculate each student's score based on the report feedback and then return the top k students ranked in non-increasing order by their score. In case of a tie in the score, the student with the lower ID should rank higher.
Key Insights
- Use sets for positive and negative feedback words to allow constant time lookups.
- Parse each report by splitting the string based on spaces and summing the scores for each word.
- Maintain a mapping between student IDs and their computed scores.
- Sort the students first by descending score and then by ascending student ID to handle tie-breakers.
- The use of a sort with a custom key or a heap can both be effective methods to obtain the top k students.
Space and Time Complexity
Time Complexity: O(n * m + n log n), where n is the number of reports and m is the average number of words per report. Space Complexity: O(n + p + q), where n is the number of students/reports, p is the number of positive words, and q is the number of negative words.
Solution
The solution involves several clear steps:
- Convert positive_feedback and negative_feedback arrays into sets for efficient lookups.
- For each report in the feedback array, split the report into words.
- Calculate the score for each report by adding 3 for every positive word and subtracting 1 for every negative word.
- Create a list of tuples (student_id, score) from the processed reports.
- Sort this list using a custom sort key that sorts by descending score and ascending student_id.
- Extract the top k student IDs from this sorted list.
This sequential approach ensures that the solution is both clear and efficient while meeting the problem constraints.