62. Unique Paths

62. Unique Paths

Dynamic Programming

class Solution {
    public int uniquePaths(int m, int n) {
        if(n ==1 && m ==1) return 1;
        int[][] grid = new int[m][n];

        for(int i = 1;  i <m; i++){
         grid[i][0] = 1;  
        }
    
        for(int i = 1; i < n; i++){
            grid[0][i] = 1;
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                grid[i][j] =grid[i-1][j] + grid[i][j-1];
            }
        }
        return grid[m-1][n-1];
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s