03. Unique Paths
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class Solution {
public:
int find(vector<vector<int>>& dp, int& m, int& n, int i, int j){
if(i==m || j==n) return 0; // Out of bounds
if(i==m-1 && j==n-1) return 1;
if(dp[i][j]!=-1) return dp[i][j];
dp[i][j] = find(dp, m, n, i+1, j) + find(dp, m, n, i, j+1);
return dp[i][j];
}
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m, vector<int>(n, -1));
return find(dp, m, n, 0, 0);
}
};
Contribution and Support
Last updated