High Five

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from collections import defaultdict
import heapq

class Solution:
    def highFive(self, items: List[List[int]]) -> List[List[int]]:
        scores = defaultdict(list)
        for id, score in items:
            heapq.heappush(scores[id], score)
            if len(scores[id]) > 5:
                heapq.heappop(scores[id])

        averages = []
        for id in sorted(scores):
            average = sum(scores[id]) // 5
            averages.append([id, average])

        return averages