-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1086. High Five.py
More file actions
40 lines (28 loc) · 992 Bytes
/
1086. High Five.py
File metadata and controls
40 lines (28 loc) · 992 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from collections import defaultdict
def highFive(items):
values = defaultdict(list)
for id, score in items:
values[id].append(score)
for k in values.keys():
values[k].sort(reverse=True)
res = []
for id, scores in values.items():
res.append([id, int(sum(scores[:5]) / 5)])
return sorted(res, key=lambda x: x[0])
items = [[1, 91], [1, 92], [2, 93], [2, 97], [1, 60], [2, 77], [1, 65], [1, 87], [1, 100], [2, 100], [2, 76]]
print(highFive(items))
import heapq
# using python's maxheap (probably unreliable)
def highFive_max_heap(items):
values = defaultdict(list)
for id, score in items:
values[id].append(score)
for k in values.keys():
heapq._heapify_max(values[k])
res = []
for id, scores in values.items():
temp = []
for _ in range(5):
temp.append(heapq._heappop_max(values[id]))
res.append([id, int(sum(temp) / 5)])
return sorted(res, key=lambda x: x[0])