LeetCode Unique Paths


A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

The original problem is here.

The original code is here.

My Solution

I solve this problem in C++, as below:

*Unique Paths 
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
using namespace std;

class Solution {
    int uniquePaths(int m, int n) {
        int matrix[100][100];
        matrix[0][0] = 1;
        for(int i=1;i<n;i++)
        for(int j=1;j<m;j++)
            matrix[j][0] = 1;
        for(int i=1;i<m;i++){
            for(int j=1;j<n;j++){
                matrix[i][j] = matrix[i-1][j] + matrix[i][j-1];
        return matrix[m-1][n-1];


Dynamic programming is used. The number of paths to get (i,j) in grid is equal to the sum of the result of (i,j-1) and (i-1,j).