Conditions | 5 |
Paths | 5 |
Total Lines | 16 |
Code Lines | 10 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
48 | public static function topKFrequent3(array $nums, int $k): array |
||
49 | { |
||
50 | if (empty($nums) || $k <= 0) { |
||
51 | return []; |
||
52 | } |
||
53 | $ans = $map = []; |
||
|
|||
54 | $map = array_count_values($nums); |
||
55 | $heap = new \SplPriorityQueue(); |
||
56 | foreach ($map as $key => $val) { |
||
57 | $heap->insert($key, $val); |
||
58 | } |
||
59 | for ($i = 0; $i < $k; $i++) { |
||
60 | array_push($ans, $heap->extract()); |
||
61 | } |
||
62 | |||
63 | return $ans; |
||
64 | } |
||
66 |