forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC0063.cpp
More file actions
executable file
·30 lines (26 loc) · 727 Bytes
/
LC0063.cpp
File metadata and controls
executable file
·30 lines (26 loc) · 727 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
28
29
30
/*
Problem Statement: https://leetcode.com/problems/unique-paths-ii/
Time: O(m • n)
Space: O(m • n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m, n;
m = obstacleGrid.size();
n = obstacleGrid[0].size();
vector<vector<int>> dp(m, vector<int>(n));
// initialization
for (int i = 0; i < m && !obstacleGrid[i][0]; i++)
dp[i][0] = 1;
for (int i = 0; i < n && !obstacleGrid[0][i]; i++)
dp[0][i] = 1;
// dynamic programming
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
if (!obstacleGrid[i][j])
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
return dp[m - 1][n - 1];
}
};