NextGreaterElementI::nextGreaterElement()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
rs 8.8333
cc 7
nc 7
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class NextGreaterElementI
8
{
9
    public static function nextGreaterElement(array $num1, array $num2): array
10
    {
11
        if (empty($num1) || empty($num2)) {
12
            return [];
13
        }
14
        $ans = $stack = $map = [];
15
        foreach ($num2 as $num) {
16
            while ($stack && end($stack) < $num) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $stack of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
17
                $map[array_pop($stack)] = $num;
18
            }
19
            array_push($stack, $num);
20
        }
21
        foreach ($num1 as $num) {
22
            array_push($ans, $map[$num] ?? -1);
23
        }
24
25
        return $ans;
26
    }
27
}
28