| Conditions | 8 |
| Paths | 9 |
| Total Lines | 17 |
| Code Lines | 10 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 28 | public static function coinChange2(array $coins, int $amount): int |
||
| 29 | { |
||
| 30 | [$m, $n] = [$amount + 1, count($coins)]; |
||
| 31 | if ($amount <= 0 || $n <= 0) { |
||
| 32 | return 0; |
||
| 33 | } |
||
| 34 | [$dp, $max] = [array_fill(0, $m, 0), PHP_INT_MAX]; |
||
| 35 | for ($i = 1; $i < $m; $i++) { |
||
| 36 | $dp[$i] = $max; |
||
| 37 | foreach ($coins as $coin) { |
||
| 38 | if ($coin <= $i && $dp[$i - $coin] !== $max) { |
||
| 39 | $dp[$i] = min($dp[$i], $dp[$i - $coin] + 1); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | return $dp[$amount] === $max ? -1 : $dp[$amount]; |
||
| 45 | } |
||
| 47 |