High Five
Real-world version: Top Five Average per ApprenticeThe 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.
def top_five_averages(records: list[list[int]]) -> list[list[int]]:public int[][] topFiveAverages(int[][] records)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 9 test cases: 9 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy
- Find Subsequence of Length K With the Largest SumReal-world version: Keeping the k Best Readings in PlaceEasy
- Top K Frequent ElementsReal-world version: Most Reported Calibration OffsetsMedium
- Task SchedulerReal-world version: Press Die CooldownMedium