-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKthSmallestElementInASortedMatrix.php
More file actions
61 lines (54 loc) · 1.57 KB
/
KthSmallestElementInASortedMatrix.php
File metadata and controls
61 lines (54 loc) · 1.57 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
namespace leetcode;
class KthSmallestElementInASortedMatrix
{
public static function kthSmallest(array $matrix, int $k): int
{
if (empty($matrix) || $k <= 0) {
return 0;
}
$heap = new \SplMaxHeap();
[$m, $n] = [count($matrix), count($matrix[0])];
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
$heap->insert($matrix[$i][$j]);
if ($heap->count() > $k) {
$heap->extract();
}
}
}
return $heap->top();
}
public static function kthSmallest2(array $matrix, int $k): int
{
if (empty($matrix) || $k <= 0) {
return 0;
}
[$m, $n] = [count($matrix), count($matrix[0])];
[$low, $high] = [$matrix[0][0], $matrix[$m - 1][$n - 1]];
$helper = static function (array $matrix, int $target) {
$n = count($matrix);
[$cnt, $i, $j] = [0, $n - 1, 0];
while ($i >= 0 && $j < $n) {
if ($matrix[$i][$j] > $target) {
$i--;
} else {
$cnt += ($i + 1);
$j++;
}
}
return $cnt;
};
while ($low < $high) {
$mid = (int)(($high - $low) / 2) + $low;
$cnt = $helper($matrix, $mid);
if ($cnt < $k) {
$low = $mid + 1;
} else {
$high = $mid - 1;
}
}
return $low;
}
}