-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniquePathsII.cpp
More file actions
27 lines (26 loc) · 863 Bytes
/
uniquePathsII.cpp
File metadata and controls
27 lines (26 loc) · 863 Bytes
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
// Source: https://leetcode.com/problems/unique-paths-ii/
// Author: Miao Zhang
// Date: 2021-01-13
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
if (obstacleGrid[m - 1][n - 1] == 1) return 0;
vector<vector<int>> dp(m, vector<int>(n, 0));
if (obstacleGrid[0][0] == 0) {
dp[0][0] = 1;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i - 1 >= 0 && obstacleGrid[i - 1][j] == 0) {
dp[i][j] += dp[i - 1][j];
}
if (j - 1 >= 0 && obstacleGrid[i][j - 1] == 0) {
dp[i][j] += dp[i][j - 1];
}
}
}
return dp[m - 1][n - 1];
}
};