Total Complexity | 14 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | class KthSmallestElementInASortedMatrix |
||
8 | { |
||
9 | public static function kthSmallest(array $matrix, int $k): int |
||
10 | { |
||
11 | if (empty($matrix) || $k <= 0) { |
||
12 | return 0; |
||
13 | } |
||
14 | $heap = new \SplMaxHeap(); |
||
15 | [$m, $n] = [count($matrix), count($matrix[0])]; |
||
16 | for ($i = 0; $i < $m; $i++) { |
||
17 | for ($j = 0; $j < $n; $j++) { |
||
18 | $heap->insert($matrix[$i][$j]); |
||
19 | if ($heap->count() > $k) { |
||
20 | $heap->extract(); |
||
21 | } |
||
22 | } |
||
23 | } |
||
24 | |||
25 | return $heap->top(); |
||
26 | } |
||
27 | |||
28 | public static function kthSmallest2(array $matrix, int $k): int |
||
60 | } |
||
61 | } |
||
62 |