Passed
Push — master ( bc7319...af3787 )
by Jinyun
02:25
created

HeightChecker::heightChecker()   A

Complexity

Conditions 6
Paths 11

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 22
rs 9.2222
cc 6
nc 11
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class HeightChecker
8
{
9
    public static function heightChecker(array $heights): int
10
    {
11
        if (empty($heights)) {
12
            return 0;
13
        }
14
        $n = count($heights);
15
        $map = array_fill(0, $n + 1, 0);
16
        foreach ($heights as $height) {
17
            $map[$height]++;
18
        }
19
        [$prev, $curr] = [0, 1];
20
        foreach ($heights as $height) {
21
            while ($map[$curr] === 0) {
22
                $curr++;
23
            }
24
            if ($curr !== $height) {
25
                $prev++;
26
            }
27
            $map[$curr]--;
28
        }
29
30
        return $prev;
31
    }
32
}
33