File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Lintcode/src/chapter4_DynamicProgrammingI Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments