High Five - Problem
Given a list of the scores of different students, items, where items[i] = [IDi, scorei] represents one score from a student with IDi, calculate each student's top five average.
Return the answer as an array of pairs result, where result[j] = [IDj, topFiveAveragej] represents the student with IDj and their top five average. Sort result by IDj in increasing order.
A student's top five average is calculated by taking the sum of their top five scores and dividing it by 5 using integer division.
Input & Output
Example 1 — Basic Case
$
Input:
items = [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]]
›
Output:
[[1,87],[2,88]]
💡 Note:
Student 1 has scores [91,92,60,65,87,100]. Top 5 are [100,92,91,87,65], average = (100+92+91+87+65)/5 = 87. Student 2 has scores [93,97,77,100,76]. Top 5 are [100,97,93,77,76], average = (100+97+93+77+76)/5 = 88.
Example 2 — Minimum Scores
$
Input:
items = [[1,100],[1,90],[1,80],[2,85],[2,95]]
›
Output:
[[1,90],[2,90]]
💡 Note:
Student 1 has only 3 scores [100,90,80], so we pad with zeros: (100+90+80+0+0)/5 = 54. Student 2 has only 2 scores [85,95], so we pad: (95+85+0+0+0)/5 = 36.
Example 3 — Single Student
$
Input:
items = [[1,70],[1,80],[1,90],[1,85],[1,95],[1,75]]
›
Output:
[[1,85]]
💡 Note:
Student 1 has 6 scores [70,80,90,85,95,75]. Top 5 are [95,90,85,80,75], average = (95+90+85+80+75)/5 = 85.
Constraints
- 1 ≤ items.length ≤ 1000
- items[i].length == 2
- 1 ≤ IDi ≤ 1000
- 0 ≤ scorei ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input
List of [studentID, score] pairs from different students
2
Process
Group by student ID and find top 5 scores for each
3
Output
Array of [studentID, topFiveAverage] sorted by ID
Key Takeaway
🎯 Key Insight: Group scores by student ID first, then efficiently maintain only the top 5 scores using a hash map and sorting or heap data structure.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code