Skip to content

Commit 7bc5102

Browse files
refactor 62
1 parent 4e7a01a commit 7bc5102

File tree

1 file changed

+18
-28
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+18
-28
lines changed
Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
package com.fishercoder.solutions;
22

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-
*/
123
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];
2922
}
30-
}
31-
return dp[m - 1][n - 1];
3223
}
33-
}
3424
}

0 commit comments

Comments
 (0)