Show problems as
Classic problem #1086EasyArrayHash TableSortingHeap (Priority Queue)

High Five

Real-world version: Top Five Average per Apprentice
Tracked in this browser only
Read High Five on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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