imajinyun /
leetcode-php
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace leetcode; |
||
| 6 | |||
| 7 | class Candy |
||
| 8 | { |
||
| 9 | public static function candy(array $ratings): int |
||
| 10 | { |
||
| 11 | $n = count($ratings); |
||
| 12 | if ($n === 0) { |
||
| 13 | return 0; |
||
| 14 | } |
||
| 15 | $nums = array_fill(0, $n, 1); |
||
| 16 | for ($i = 1; $i < $n; $i++) { |
||
| 17 | if ($ratings[$i] > $ratings[$i - 1]) { |
||
| 18 | $nums[$i] = $nums[$i - 1] + 1; |
||
| 19 | } |
||
| 20 | } |
||
| 21 | for ($i = $n - 2; $i >= 0; $i--) { |
||
| 22 | if ($ratings[$i] > $ratings[$i + 1]) { |
||
| 23 | $nums[$i] = max($nums[$i], $nums[$i + 1] + 1); |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | return array_sum($nums); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 28 | } |
||
| 29 | } |
||
| 30 |