|
1 | 1 | package com.fishercoder.solutions; |
2 | 2 |
|
3 | | -/** |
4 | | - * 62. Unique Paths |
5 | | -
|
6 | | -A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). |
7 | | -The robot can only move either down or right at any point in time. The robot is trying to reach |
8 | | -the bottom-right corner of the grid (marked 'Finish' in the diagram below). |
9 | | -
|
10 | | -How many possible unique paths are there? |
11 | | - */ |
12 | 3 | public class _62 { |
13 | | - |
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 | | - } |
| 4 | + public static class Solution1 { |
| 5 | + /** |
| 6 | + * Another typical DP question, use a 2d array: the first row and the first column need to be |
| 7 | + * initialized to be 1 since there's only one way to reach every position in the first row and |
| 8 | + * the first column: either from left or top. |
| 9 | + */ |
| 10 | + public int uniquePaths(int m, int n) { |
| 11 | + int[][] dp = new int[m][n]; |
| 12 | + for (int i = 0; i < m; i++) { |
| 13 | + for (int j = 0; j < n; j++) { |
| 14 | + if (i == 0 || j == 0) { |
| 15 | + dp[i][j] = 1; |
| 16 | + } else { |
| 17 | + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + return dp[m - 1][n - 1]; |
29 | 22 | } |
30 | | - } |
31 | | - return dp[m - 1][n - 1]; |
32 | 23 | } |
33 | | - } |
34 | 24 | } |
0 commit comments