Skip to content

Commit 2f34d85

Browse files
Lintcode/src/chapter4_DynamicProgrammingI/UniquePaths.java
1 parent daa7898 commit 2f34d85

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package chapter4_DynamicProgrammingI;
2+
3+
import utils.CommonUtils;
4+
5+
public class UniquePaths {
6+
7+
/**
8+
* @param n, m: positive integer (1 <= n ,m <= 100)
9+
* @return an integer
10+
*/
11+
public static int uniquePaths(int m, int n) {
12+
// write your code here
13+
int[][] ways = new int[m][n];
14+
//initialize row 0
15+
for(int i = 0; i < n; i++) ways[0][i] = 1;
16+
17+
//initialize col 0
18+
for(int i = 0; i < m; i++) ways[i][0] = 1;
19+
20+
for(int i = 1; i < m; i++){
21+
for(int j = 1; j < n; j++){
22+
ways[i][j] = ways[i-1][j] + ways[i][j-1];
23+
}
24+
}
25+
26+
CommonUtils.printMatrix(ways);
27+
return ways[m-1][n-1];
28+
}
29+
30+
31+
public static void main(String...strings){
32+
System.out.print(uniquePaths(2, 62));
33+
}
34+
}

0 commit comments

Comments
 (0)