Leetcode 2512: Reward Top K Students

grid47
grid47
Exploring patterns and algorithms
Feb 29, 2024 9 min read

You are given two lists of words: one representing positive feedback and the other representing negative feedback. Each feedback word affects a student’s points: a positive word adds 3 points, while a negative word subtracts 1 point. For each feedback report, which is associated with a student, calculate the total score by counting the occurrences of positive and negative words. After evaluating all reports, return the top k students based on their total points. In case of a tie in points, the student with the lower ID should rank higher.
Problem
Approach
Steps
Complexity
Input: You are given the following inputs: - `positive_feedback`: A list of words indicating positive feedback. - `negative_feedback`: A list of words indicating negative feedback. - `report`: A list of feedback reports for students. - `student_id`: A list of student IDs corresponding to each report. - `k`: An integer specifying how many top students to return based on their scores.
Example: positive_feedback = ["creative", "enthusiastic", "hardworking"], negative_feedback = ["lazy"], report = ["the student is hardworking and lazy", "the student is creative and enthusiastic"], student_id = [1, 2], k = 2
Constraints:
• 1 <= positive_feedback.length, negative_feedback.length <= 10^4
• 1 <= report[i].length <= 100
• 1 <= student_id[i] <= 10^9
• 1 <= k <= n
Output: Return the top k student IDs ranked by their points. If multiple students have the same points, return the one with the lower student ID first.
Example: [2, 1]
Constraints:
• Return the IDs of the top k students.
Goal: Calculate the total score for each student based on the feedback words, sort them in descending order by score, and then return the top k students.
Steps:
• 1. Initialize sets for positive and negative feedback words.
• 2. Loop through each report and calculate the score for each student by checking the occurrence of positive and negative words.
• 3. Use a priority queue to sort the students by their scores in descending order, prioritizing lower IDs in case of a tie.
• 4. Extract the IDs of the top k students from the priority queue.
Goal: Ensure all inputs adhere to the specified ranges and constraints.
Steps:
• 1 <= positive_feedback.length, negative_feedback.length <= 10^4
• 1 <= report[i].length <= 100
• 1 <= student_id[i] <= 10^9
• 1 <= k <= n
Assumptions:
• All student IDs are unique.
• Words in the report are separated by a single space.
• There are no overlapping words between the positive and negative feedback lists.
Input: positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is studious", "the student is smart"], student_id = [1, 2], k = 2
Explanation: In this case, both students have the same number of positive feedback (3 points), but since student 1 has a lower ID, he ranks higher. The output is [1, 2].

Input: positive_feedback = ["smart","brilliant","studious"], negative_feedback = ["not"], report = ["this student is not studious", "the student is smart"], student_id = [1, 2], k = 2
Explanation: Student 1 has 3 points (1 positive, 1 negative), while student 2 has 3 points (1 positive). Since student 2 has more points, the output is [2, 1].

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus