Leetcode 63 Solution
This article provides solution to leetcode question 63 (unique-paths-ii)
Access this page by simply typing in "lcs 63" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/unique-paths-ii
Solution
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
if (m == 0)
return 0;
int n = obstacleGrid[0].size();
vector<vector<int>> a;
for (int i = 0; i < m; i++)
a.push_back(vector<int>(n));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (obstacleGrid[i][j] == 1)
a[i][j] = 0;
else if (i == 0 && j == 0)
a[i][j] = 1;
else
{
int s = 0;
if (i > 0 && obstacleGrid[i - 1][j] == 0)
s += a[i - 1][j];
if (j > 0 && obstacleGrid[i][j - 1] == 0)
s += a[i][j - 1];
a[i][j] = s;
}
}
}
return a[m - 1][n - 1];
}
};