Joey LIU | NANTSOU


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    public int uniquePaths(int m, int n) {
        int[][] mat = new int[m][n];
        
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 || j == 0) {
                    // key point: the number of the positions in first row or first column is 1.
                    mat[i][j] = 1;
                } else {
                    // key point: the total number of current position is the sum of the previous positions.
                    mat[i][j] = mat[i - 1][j] + mat[i][j - 1];
                }
            }
        }
        return mat[m - 1][n - 1];
    }
}