Skip to content

Instantly share code, notes, and snippets.

@pdu
Created February 16, 2013 11:54
Show Gist options
  • Select an option

  • Save pdu/4966588 to your computer and use it in GitHub Desktop.

Select an option

Save pdu/4966588 to your computer and use it in GitHub Desktop.
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? Above is a 3 x 7 grid. How many poss…
#define MAXN 100
int ans[MAXN][MAXN];
class Solution {
public:
int uniquePaths(int m, int n) {
for (int i = 1; i <= m; ++i)
for (int j = 1; j <= n; ++j) {
if (i == 1 && j == 1)
ans[i][j] = 1;
else
ans[i][j] = 0;
if (i - 1 > 0)
ans[i][j] += ans[i - 1][j];
if (j - 1 > 0)
ans[i][j] += ans[i][j - 1];
}
return ans[m][n];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment