SearchInsertPosition::searchInsert()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
c 3
b 0
f 0
dl 0
loc 19
rs 9.5222
cc 5
nc 5
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class SearchInsertPosition
8
{
9
    public static function searchInsert(array $nums, int $target): int
10
    {
11
        if (empty($nums)) {
12
            return 0;
13
        }
14
        [$low, $high] = [0, count($nums) - 1];
15
        while ($low <= $high) {
16
            $mid = (int) (($high + $low) / 2);
0 ignored issues
show
Unused Code introduced by
The assignment to $mid is dead and can be removed.
Loading history...
17
            $mid = $low + intdiv($high - $low, 2);
18
            if ($nums[$mid] > $target) {
19
                $high = $mid - 1;
20
            } elseif ($nums[$mid] < $target) {
21
                $low = $mid + 1;
22
            } else {
23
                return $mid;
24
            }
25
        }
26
27
        return $low;
28
    }
29
}
30