-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHighFive.php
More file actions
38 lines (34 loc) · 892 Bytes
/
HighFive.php
File metadata and controls
38 lines (34 loc) · 892 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
<?php
declare(strict_types=1);
namespace leetcode;
class HighFive
{
public static function highFive(array $items): array
{
if (empty($items)) {
return [];
}
$map = [];
foreach ($items as $item) {
[$sid, $score] = $item;
$scores = $map[$sid] ?? array_fill(0, 5, 0);
$j = 0;
for ($i = 1; $i < 5; $i++) {
if ($scores[$i] < $scores[$j]) {
$j = $i;
}
}
if ($score > $scores[$j]) {
$scores[$j] = $score;
}
$map[$sid] = $scores;
}
[$ans, $keys] = [[], array_keys($map)];
sort($keys);
foreach ($keys as $sid) {
$sum = (int) (array_sum($map[$sid]) / 5);
array_push($ans, [$sid, $sum]);
}
return $ans;
}
}