ThirdMaximumNumber   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 17
c 2
b 0
f 0
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C thirdMax() 0 23 13
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ThirdMaximumNumber
8
{
9
    public static function thirdMax(array $nums): int
10
    {
11
        if (empty($nums)) {
12
            return 0;
13
        }
14
        $a = $b = $c = null;
15
        foreach ($nums as $num) {
16
            if ($num === $a || $num === $b || $num === $c) {
17
                continue;
18
            }
19
            if ($a === null || $num > $a) {
20
                $c = $b;
21
                $b = $a;
22
                $a = $num;
23
            } elseif ($b === null || $num > $b) {
24
                $c = $b;
25
                $b = $num;
26
            } elseif ($c === null || $num > $c) {
27
                $c = $num;
28
            }
29
        }
30
31
        return $c === null ? $a : $c;
0 ignored issues
show
introduced by
The condition $c === null is always true.
Loading history...
32
    }
33
}
34