[
array
hash table
sort
]
Leetcode 1086 High Five
Problem statement
https://leetcode.com/problems/high-five/
Solution
For each studend keep all scores he has and then sort ids of students and for each student take last 5
elements of sorted scores.
Complexity
It is O(n log n)
, where n
is total number of all scores.
We can use heap as well to keap top 5 marks.
Code
class Solution:
def highFive(self, items):
d = defaultdict(list)
for a, b in items:
d[a] += [b]
return [[a, sum(sorted(d[a])[-5:])//5] for a in sorted(d.keys())]