Skip to content

Commit c00c4cf

Browse files
committed
Unique Paths
1 parent 6eb686a commit c00c4cf

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

UniquePaths.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in
3+
* the diagram below).
4+
*
5+
* The robot can only move either down or right at any point in time. The robot
6+
* is trying to reach the bottom-right corner of the grid (marked 'Finish' in
7+
* the diagram below).
8+
*
9+
* How many possible unique paths are there?
10+
*
11+
* Above is a 3 x 7 grid. How many possible unique paths are there?
12+
*
13+
* Note: m and n will be at most 100.
14+
*
15+
*/
16+
17+
public class UniquePaths {
18+
public int uniquePaths(int m, int n) {
19+
if (m == 0 || n == 0)
20+
return 0;
21+
int[][] map = new int[m][n];
22+
for (int i = 0; i < m; i++) {
23+
map[i][0] = 1;
24+
}
25+
for (int j = 0; j < n; j++) {
26+
map[0][j] = 1;
27+
}
28+
for (int i = 1; i < m; i++) {
29+
for (int j = 1; j < n; j++) {
30+
map[i][j] = map[i - 1][j] + map[i][j - 1];
31+
}
32+
}
33+
return map[m - 1][n - 1];
34+
}
35+
}

0 commit comments

Comments
 (0)