All problems
0694EasyArrayHash TableSortingHeap (Priority Queue)

Top Five Average per Apprentice

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1086High Five

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A workshop logs one entry per weld inspection. Entry records[i] = [badge, score] says that the apprentice wearing badge scored score on one inspection. An apprentice may appear many times, and the entries are in no particular order.

For every badge that appears, take that apprentice's five highest scores and average them, rounding the average down to a whole number. Repeated scores count separately, so an apprentice with five inspections all scored 80 has five scores of 80.

Return one entry [badge, average] per badge that appears, ordered by badge from smallest to largest.

Examples

Example 1

Input
records = [[7, 100], [7, 100], [7, 100], [7, 100], [7, 99]]
Output
[[7, 99]]

Badge 7 has exactly five scores, adding to 499. Dividing by five and rounding down gives 99.

Example 2

Input
records = [[1, 90], [1, 80], [1, 70], [1, 60], [1, 50], [1, 40]]
Output
[[1, 70]]

The score of 40 is not among the badge's five highest, so the average is taken over 90, 80, 70, 60 and 50, which add to 350.

Example 3

Input
records = [[1000, 0], [1000, 0], [1000, 0], [1000, 0], [1000, 0], [3, 100], [3, 100], [3, 100], [3, 100], [3, 100]]
Output
[[3, 100], [1000, 0]]

Badge 3 averages 100 and badge 1000 averages 0. Badge 3 is listed first because the answer is ordered by badge, not by where the entries appear in the log.

Constraints

  • 5 <= records.length <= 1000
  • records[i].length == 2
  • 0 <= records[i][j] <= 1000
  • Each entry is [badge, score] with 1 <= badge <= 1000 and 0 <= score <= 100.
  • Every badge that appears has at least five entries.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def top_five_averages(records: list[list[int]]) -> list[list[int]]:
Java
public int[][] topFiveAverages(int[][] records)
September 7
Apply