-
Notifications
You must be signed in to change notification settings - Fork 112
/
063-UniquePaths2.cs
44 lines (38 loc) · 1.52 KB
/
063-UniquePaths2.cs
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
44
//-----------------------------------------------------------------------------
// Runtime: 160ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _063_UniquePaths2
{
public int UniquePathsWithObstacles(int[,] obstacleGrid)
{
var rowLenght = obstacleGrid.GetLength(0);
var columnLenght = obstacleGrid.GetLength(1);
if (rowLenght <= columnLenght)
{
var possiblePath = new int[rowLenght];
possiblePath[0] = obstacleGrid[0, 0] == 0 ? 1 : 0;
for (int i = 0; i < columnLenght; i++)
for (int j = 0; j < rowLenght; j++)
possiblePath[j] = obstacleGrid[j, i] == 1
? 0
: j != 0 ? possiblePath[j - 1] + possiblePath[j] : possiblePath[j];
return possiblePath[rowLenght - 1];
}
else
{
var possiblePath = new int[columnLenght];
possiblePath[0] = obstacleGrid[0, 0] == 0 ? 1 : 0;
for (int i = 0; i < rowLenght; i++)
for (int j = 0; j < columnLenght; j++)
possiblePath[j] = obstacleGrid[i, j] == 1
? 0
: j != 0 ? possiblePath[j - 1] + possiblePath[j] : possiblePath[j];
return possiblePath[columnLenght - 1];
}
}
}
}