Reward Top K Students - Complete Solution Guide
Reward Top K Students is LeetCode problem 2512, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 , whe
Detailed Explanation
The problem requires ranking students based on feedback they receive. Each student has a unique ID, and receives a feedback report consisting of words. A positive word increases their score by 3, while a negative word decreases it by 1. The goal is to return the IDs of the top `k` students, ranked in non-increasing order of their scores. If two students have the same score, the student with the lower ID ranks higher.
Solution Approach
The solution calculates each student's score by iterating through their feedback report, checking if each word is in the set of positive or negative words, and updating the score accordingly. It then stores the score and ID of each student in a list of pairs. This list is sorted based on the score (in descending order) and, if the scores are equal, based on the student ID (in ascending order). Finally, the first `k` student IDs from the sorted list are returned.
Step-by-Step Algorithm
- Step 1: Create hash sets (or sets) for positive and negative feedback words for efficient lookup.
- Step 2: Iterate through each student's report and calculate their score. For each word in the report, check if it's in the positive or negative feedback set and update the score accordingly.
- Step 3: Store each student's score and ID as a pair in a list.
- Step 4: Sort the list of student information based on the score in descending order (using a negative score for simpler sorting) and then by student ID in ascending order (for tie-breaking).
- Step 5: Extract the top `k` student IDs from the sorted list and return them.
Key Insights
- Insight 1: Representing positive and negative words efficiently using hash sets (or sets in Python) allows for quick lookup when calculating the score for each student.
- Insight 2: Storing student information (score and ID) as a pair (or a similar data structure) is crucial for sorting them based on score and ID.
- Insight 3: Using negative scores for sorting allows us to sort in ascending order and directly pick the top `k` students without reversing the order later.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, String, Sorting, Heap (Priority Queue).
Companies
Asked at: Booking.com.