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

HeightChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A heightChecker() 0 22 6
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