Test Failed
Push — master ( d4a23a...567b28 )
by Jinyun
11:27
created

IntersectionOfTwoArrays::intersection3()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 29
rs 8.0555
cc 9
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class IntersectionOfTwoArrays
8
{
9
    public static function intersection(array $p, array $q): array
10
    {
11
        if (empty($p) || empty($q)) {
12
            return [];
13
        }
14
        $map = $set = [];
15
        foreach ($p as $v) {
16
            if (!self::contains($map, $v)) {
17
                array_push($map, $v);
18
            }
19
        }
20
        foreach ($q as $v) {
21
            if (self::contains($map, $v) && !self::contains($set, $v)) {
22
                array_push($set, $v);
23
            }
24
        }
25
26
        return $set;
27
    }
28
29
    public static function intersection2(array $p, array $q): array
30
    {
31
        if (empty($p) || empty($q)) {
32
            return [];
33
        }
34
        sort($p);
35
        sort($q);
36
        $i = $j = 0;
37
        [$m, $n] = [count($p), count($q)];
38
        $ans = $set = [];
39
        while ($i < $m && $j < $n) {
40
            [$v1, $v2] = [$p[$i], $q[$j]];
41
            if ($v1 < $v2) {
42
                $i++;
43
            } elseif ($v1 > $v2) {
44
                $j++;
45
            } else {
46
                if (!self::contains($set, $v1)) {
47
                    array_push($set, $v1);
48
                }
49
                $i++;
50
                $j++;
51
            }
52
        }
53
54
        foreach ($p as $v) {
55
            if (self::contains($set, $v) && !self::contains($ans, $v)) {
56
                array_push($ans, $v);
57
            }
58
        }
59
60
        return $ans;
61
    }
62
63
    public static function intersection3(array $p, array $q): array
64
    {
65
        if (empty($p) || empty($q)) {
66
            return [];
67
        }
68
        $helper = static function (array $nums, int $target): bool {
69
            [$low, $high] = [0, count($nums) - 1];
70
            while ($low <= $high) {
71
                $mid = $low + intdiv($high - $low, 2);
72
                if ($nums[$mid] > $target) {
73
                    $high = $mid - 1;
74
                } elseif ($nums[$mid] < $target) {
75
                    $low = $mid + 1;
76
                } else {
77
                    return true;
78
                }
79
            }
80
81
            return false;
82
        };
83
        sort($p);
84
        $ans = [];
85
        foreach ($q as $v) {
86
            if ($helper($p, $v) && !self::contains($ans, $v)) {
87
                array_push($ans, $v);
88
            }
89
        }
90
91
        return $ans;
92
    }
93
94
    private static function contains(array $array, int $value): bool
95
    {
96
        return in_array($value, $array, true);
97
    }
98
}
99