Test Failed
Push — master ( b25d9d...a76292 )
by Jinyun
02:16
created

FirstBadVersion::firstBadVersion()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 16
rs 9.6111
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class FirstBadVersion
8
{
9
    public static function firstBadVersion(int $m, int $n): int
10
    {
11
        if ($m <= 0 || $n <= 0) {
12
            return 0;
13
        }
14
        [$low, $high] = [1, $m];
15
        while ($low <= $high) {
16
            $mid = $low + (int)(($high - $low) / 2);
17
            if (self::isBadVersion($mid, $n)) {
18
                $high = $mid - 1;
19
            } else {
20
                $low = $mid + 1;
21
            }
22
        }
23
24
        return $low;
25
    }
26
27
    public static function isBadVersion(int $m, int $n): bool
28
    {
29
        $map = array_fill(0, $n + 1, false);
30
        $map[$n] = true;
31
32
        return $map[$m];
33
    }
34
}
35