-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path63.php
43 lines (38 loc) · 1.03 KB
/
63.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/**
* Class Solution
* 63.不同路径 II
* @link https://leetcode-cn.com/problems/unique-paths-ii/
*/
class Solution
{
/**
* @param Integer[][] $obstacleGrid
* @return Integer
*/
function uniquePathsWithObstacles($obstacleGrid)
{
$row = count($obstacleGrid);
$col = count(current($obstacleGrid));
$dp = array_fill(0, $row, array_fill(0, $col, 0));
$dp[0][0] = $obstacleGrid[0][0] == 1 ? 0 : 1;
for ($i = 0; $i < $row; $i++) {
for ($j = 0; $j < $col; $j++) {
if ($obstacleGrid[$i][$j] == 1) {
continue;
}
$top = $j - 1;
$left = $i - 1;
if ($top >= 0) {
$dp[$i][$j] += $dp[$i][$top];
}
if ($left >= 0) {
$dp[$i][$j] += $dp[$left][$j];
}
}
}
return $dp[$row - 1][$col - 1];
}
}
$obj = new Solution();
echo $obj->uniquePathsWithObstacles([[1, 0]]);