|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import com.fishercoder.common.utils.CommonUtils; |
4 |
| - |
5 |
| -/**Unique Paths |
| 3 | +/** |
| 4 | + * 62. Unique Paths |
6 | 5 |
|
7 | 6 | A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
|
8 | 7 | The robot can only move either down or right at any point in time. The robot is trying to reach
|
9 | 8 | the bottom-right corner of the grid (marked 'Finish' in the diagram below).
|
10 | 9 |
|
11 |
| -How many possible unique paths are there?*/ |
| 10 | +How many possible unique paths are there? |
| 11 | + */ |
12 | 12 | public class _62 {
|
13 |
| - |
14 |
| - /**Another typical DP question, use a 2d array: |
15 |
| - * the first row and the first column need to be initialized to be 1 since there's only one way to reach every |
16 |
| - * position in the first row and the first column: either from left or top.*/ |
17 |
| - public int uniquePaths(int m, int n) { |
18 |
| - int[][] dp = new int[m][n]; |
19 |
| - for (int i = 0; i < m; i++) { |
20 |
| - dp[i][0] = 1; |
21 |
| - } |
22 |
| - for (int i = 0; i < n; i++) { |
23 |
| - dp[0][i] = 1; |
24 |
| - } |
25 |
| - |
26 |
| - for (int i = 1; i < m; i++) { |
27 |
| - for (int j = 1; j < n; j++) { |
28 |
| - int ways = 0; |
29 |
| - if (i - 1 >= 0) { |
30 |
| - ways += dp[i - 1][j]; |
31 |
| - } |
32 |
| - if (j - 1 >= 0) { |
33 |
| - ways += dp[i][j - 1]; |
34 |
| - } |
35 |
| - dp[i][j] = ways; |
36 |
| - } |
37 |
| - } |
38 |
| - CommonUtils.printMatrix(dp); |
39 |
| - return dp[m - 1][n - 1]; |
40 |
| - } |
41 | 13 |
|
42 |
| - //and we can actually put the two initialization for loop into the one |
43 |
| - public int uniquePaths_merged_for_loop(int m, int n) { |
44 |
| - int[][] dp = new int[m][n]; |
45 |
| - for (int i = 0; i < m; i++) { |
46 |
| - for (int j = 0; j < n; j++) { |
47 |
| - if (i == 0 || j == 0) { |
48 |
| - dp[i][j] = 1; |
49 |
| - } else { |
50 |
| - dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; |
51 |
| - } |
52 |
| - } |
| 14 | + public static class Solution1 { |
| 15 | + /** |
| 16 | + * Another typical DP question, use a 2d array: the first row and the first column need to be |
| 17 | + * initialized to be 1 since there's only one way to reach every position in the first row and |
| 18 | + * the first column: either from left or top. |
| 19 | + */ |
| 20 | + public int uniquePaths(int m, int n) { |
| 21 | + int[][] dp = new int[m][n]; |
| 22 | + for (int i = 0; i < m; i++) { |
| 23 | + for (int j = 0; j < n; j++) { |
| 24 | + if (i == 0 || j == 0) { |
| 25 | + dp[i][j] = 1; |
| 26 | + } else { |
| 27 | + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; |
| 28 | + } |
53 | 29 | }
|
54 |
| - return dp[m - 1][n - 1]; |
55 |
| - } |
56 |
| - |
57 |
| - public static void main(String... strings) { |
58 |
| - _62 test = new _62(); |
59 |
| - int m = 1; |
60 |
| - int n = 2; |
61 |
| - System.out.println(test.uniquePaths(m, n)); |
| 30 | + } |
| 31 | + return dp[m - 1][n - 1]; |
62 | 32 | }
|
| 33 | + } |
63 | 34 | }
|
0 commit comments