imajinyun /
leetcode-php
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace leetcode; |
||
| 6 | |||
| 7 | class NumberOfEquivalentDominoPairs |
||
| 8 | { |
||
| 9 | public static function numEquivDominoPairs(array $dominoes): int |
||
| 10 | { |
||
| 11 | [$m, $n] = [count($dominoes), is_array($dominoes) ? count($dominoes[0]) : 0]; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 12 | if ($m === 0 || $n === 0) { |
||
| 13 | return 0; |
||
| 14 | } |
||
| 15 | [$cnt, $map] = [0, []]; |
||
| 16 | foreach ($dominoes as $dominoe) { |
||
| 17 | $max = max($dominoe[0], $dominoe[1], $min = min($dominoe[0], $dominoe[1])); |
||
| 18 | $key = $min * 10 + $max; |
||
| 19 | $pairs = $map[$key] ?? 0; |
||
| 20 | $map[$key] = 1 + $pairs; |
||
| 21 | $cnt += $pairs; |
||
| 22 | } |
||
| 23 | |||
| 24 | return $cnt; |
||
| 25 | } |
||
| 26 | } |
||
| 27 |