| Total Complexity | 15 |
| Total Lines | 38 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class CoinChange |
||
| 8 | { |
||
| 9 | public static function coinChange(array $coins, int $amount): int |
||
| 10 | { |
||
| 11 | [$m, $n] = [$amount + 1, count($coins)]; |
||
| 12 | if ($amount <= 0 || $n <= 0) { |
||
| 13 | return 0; |
||
| 14 | } |
||
| 15 | [$dp, $dp[0]] = [array_fill(0, $m, $m), 0]; |
||
| 16 | for ($i = 1; $i < $m; $i++) { |
||
| 17 | for ($j = 0; $j < $n; $j++) { |
||
| 18 | $coin = $coins[$j]; |
||
| 19 | if ($coin <= $i) { |
||
| 20 | $dp[$i] = min($dp[$i], $dp[$i - $coin] + 1); |
||
| 21 | } |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | return $dp[$amount] > $amount ? -1 : $dp[$amount]; |
||
| 26 | } |
||
| 27 | |||
| 28 | public static function coinChange2(array $coins, int $amount): int |
||
| 45 | } |
||
| 46 | } |
||
| 47 |