leetcode 62, unique paths

https://leetcode.com/problems/unique-paths/description/?envType=daily-question&envId=2023-09-03
mail@pastecode.io avatarunknown
c_cpp
a month ago
674 B
7
Indexable
Never
// AMAN JAIN MCA 1st YEAR 2nd SEM

// time O(N*M), space (N*M)
// Approach: Basic bottom up dynamic programming
class Solution {
public:
    int uniquePaths(int m, int n) {
        int wayCount[m][n];
        for(int i = 0; i < m; ++i) {
            for(int j = 0; j < n; ++j) {
                if(i == 0) wayCount[i][j] = 1;
                else {
                    int myCount = 0;
                    if(i - 1 >= 0) myCount += wayCount[i - 1][j];
                    if(j - 1 >= 0) myCount += wayCount[i][j - 1];
                    wayCount[i][j] = myCount;
                }
            }
        }
        return wayCount[m - 1][n - 1];
    }
};