| Total Complexity | 15 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class UniquePaths |
||
| 8 | { |
||
| 9 | public static function uniquePaths(int $m, int $n): int |
||
| 10 | { |
||
| 11 | if ($m <= 0 || $n <= 0) { |
||
| 12 | return 0; |
||
| 13 | } |
||
| 14 | $dp = array_fill(0, $m, array_fill(0, $n, 1)); |
||
| 15 | for ($i = 1; $i < $m; $i++) { |
||
| 16 | for ($j = 1; $j < $n; $j++) { |
||
| 17 | $dp[$i][$j] = $dp[$i - 1][$j] + $dp[$i][$j - 1]; |
||
| 18 | } |
||
| 19 | } |
||
| 20 | |||
| 21 | return $dp[$m - 1][$n - 1]; |
||
| 22 | } |
||
| 23 | |||
| 24 | public static function uniquePaths2(int $m, int $n): int |
||
| 25 | { |
||
| 26 | if ($m <= 0 || $n <= 0) { |
||
| 27 | return 0; |
||
| 28 | } |
||
| 29 | $prev = $curr = array_fill(0, $n, 1); |
||
| 30 | for ($i = 1; $i < $m; $i++) { |
||
| 31 | for ($j = 1; $j < $n; $j++) { |
||
| 32 | $curr[$j] = $prev[$j] + $curr[$j - 1]; |
||
| 33 | } |
||
| 34 | $temp = $prev; |
||
| 35 | $prev = $curr; |
||
| 36 | $curr = $temp; |
||
| 37 | } |
||
| 38 | |||
| 39 | return $prev[$n - 1]; |
||
| 40 | } |
||
| 41 | |||
| 42 | public static function uniquePaths3(int $m, int $n): int |
||
| 55 | } |
||
| 56 | } |
||
| 57 |