This problem asks us to calculate the top five average score for each student given a list of student IDs and their scores. The solution involves several steps:
Data Organization: We need to efficiently store and access scores for each student. A hash map (dictionary in Python, HashMap in Java, etc.) is ideal for this. The keys will be student IDs, and the values will be lists of their scores.
Top Five Scores: For each student, we need to find their top five scores. Sorting the scores in descending order and taking the first five is one way to achieve this. Alternatively, we can use a min-heap (priority queue) of size 5; we add each score to the heap, removing the smallest if the heap size exceeds 5. This keeps the heap containing the top 5 scores efficiently.
Average Calculation: After identifying the top five scores, we calculate their average using integer division.
Result Formatting: The final result must be an array of pairs [ID, average]
, sorted by ID.
Time Complexity:
Hash map based approach: The time complexity is dominated by iterating through the input list (items
). Building the hash map takes O(N) time, where N is the length of items
. Sorting the scores for each student takes O(M log M) time in the worst case, where M is the maximum number of scores for a single student. If we consider the total number of scores across all students as T
, sorting all scores takes O(T log T) time. The overall time complexity is therefore O(N + T log T), or O(T log T) if N is smaller than T.
Min-heap based approach: Building the min-heap for each student takes O(M log 5) which simplifies to O(M) in the average case and O(M log M) in the worst case. The overall time complexity in this case would be O(N + T) which is close to linear time. In this case the time complexity is linear.
Space Complexity:
The space complexity is determined primarily by the hash map, which stores the scores for each student. In the worst case, where each student has many scores, the space complexity could be O(T). In the min-heap approach it is O(N) because it only stores a maximum of five scores for each student, and hence the space complexity is linear.
The provided code snippets use various programming languages. Let's break down one approach (using a min-heap) with a Java example:
class Solution {
public int[][] highFive(int[][] items) {
// Use a HashMap to store scores for each student
Map<Integer, PriorityQueue<Integer>> studentScores = new HashMap<>();
// Iterate through the input and add scores to the priority queue (min-heap)
for (int[] item : items) {
int studentId = item[0];
int score = item[1];
studentScores.putIfAbsent(studentId, new PriorityQueue<>()); // Initialize if not present
studentScores.get(studentId).offer(score); // Add score to heap
if (studentScores.get(studentId).size() > 5) {
studentScores.get(studentId).poll(); // Remove smallest if more than 5
}
}
// Prepare the result array
int[][] result = new int[studentScores.size()][2];
int index = 0;
// Iterate through the HashMap, calculate the average, and add to the result
for (Map.Entry<Integer, PriorityQueue<Integer>> entry : studentScores.entrySet()) {
int studentId = entry.getKey();
PriorityQueue<Integer> scores = entry.getValue();
int sum = 0;
for (int score : scores) {
sum += score;
}
result[index][0] = studentId;
result[index++][1] = sum / 5;
}
Arrays.sort(result, (a, b) -> a[0] - b[0]); // Sort by student ID
return result;
}
}
This Java code uses a HashMap
to store student scores and a PriorityQueue
(min-heap) to efficiently manage the top 5 scores for each student. The final result array is sorted by student ID. Other language implementations follow similar logic, with variations in syntax and data structures. The Python code uses defaultdict
for efficient hash map creation and nlargest
for getting the top 5 scores. The C++ and Go versions employ standard library sorting functions and similar data structures to achieve the same outcome. The TypeScript version also leverages sorting and array operations.