RelativeSortArray   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B relativeSortArray() 0 23 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class RelativeSortArray
8
{
9
    public static function relativeSortArray(array $a, array $b): array
10
    {
11
        if (empty($a) || empty($b)) {
12
            return [];
13
        }
14
        [$n, $k] = [1001, 0];
15
        $cnt = array_fill(0, $n, 0);
16
        $ans = [];
17
        foreach ($a as $v) {
18
            $cnt[$v] = ($cnt[$v] ?? 0) + 1;
19
        }
20
        foreach ($b as $v) {
21
            while ($cnt[$v]-- > 0) {
22
                $ans[$k++] = $v;
23
            }
24
        }
25
        for ($i = 0; $i < $n; $i++) {
26
            while (isset($cnt[$i]) && $cnt[$i]-- > 0) {
27
                $ans[$k++] = $i;
28
            }
29
        }
30
31
        return $ans;
32
    }
33
}
34