[InterviewBit] Unique Paths in a Grid

Unique Paths in a Grid

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int Solution::uniquePathsWithObstacles(vector<vector<int> > &A) {
if(A[0][0] == 1) return 0;
int n = A.size(), m = A[0].size();
if(A[n-1][m-1] == 1) return 0;
vector<vector<int>> dp(n + 1, vector<int>(m + 1,0));
dp[0][0] = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(A[i][j]) continue;
dp[i+1][j] += dp[i][j];
dp[i][j+1] += dp[i][j];
}
}
return dp[n-1][m-1];
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/10/PS/interviewbit/unique-paths-in-a-grid/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.