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
|
|||
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 |
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.