# QUESTION

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?

This question is provided by Leetocode.

# SOLUTION

It’s catalan number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Solution { public: int uniquePaths(int m, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if (0 == m || 0 == n) return 0; vector<int> dp(m, 1); for (int j = 1; j < n; ++j) { for (int i = 1; i < m; ++i) { dp[i] += dp[i - 1]; } } return dp[m - 1]; } }; |