diff --git a/62. Unique Paths/solution1_1.java b/62. Unique Paths/solution1_1.java new file mode 100644 index 0000000..28861fc --- /dev/null +++ b/62. Unique Paths/solution1_1.java @@ -0,0 +1,36 @@ +/* + * ・概要 + * 自力解法 + * ゴールからPathを1つずつ返す方法がすぐに思いついたので再帰で実装。 + * メモ化しないと何回も同じ位置で再帰をしていまうことになるが一旦実装。 + * + * ・計算量 + * 時間:O(2^(m+n)) + * 空間:O(m+n) + * + * ・その他 + * あとからみかえすとl24, l27行目は不要そう。limitRow、limitColも微妙かも、goalRowとかのがいいかも? + */ + +public class solution1_1 { + public int uniquePaths(int m, int n) { + if (m < 0 || n < 0) { + return 0; + } + return countPath(0, 0, m - 1, n - 1); + } + + private int countPath(int row, int col, int limitRow, int limitCol) { + if (row < 0 || limitRow < row) { + return 0; + } + if (col < 0 || limitCol < col) { + return 0; + } + if (row == limitRow && col == limitCol) { + return 1; + } + return countPath(row + 1, col, limitRow, limitCol) + + countPath(row, col + 1, limitRow, limitCol); + } +} diff --git a/62. Unique Paths/solution1_2.java b/62. Unique Paths/solution1_2.java new file mode 100644 index 0000000..7316b27 --- /dev/null +++ b/62. Unique Paths/solution1_2.java @@ -0,0 +1,34 @@ +/* + * ・概要 + * 自力解法 + * solution1_1のメモ化バージョン + * + * ・計算量 + * 時間:O(n*m), 各マス1回ずつしか走査しなくてすむので + * 空間:O(n*m) + */ + +public class soluion1_2 { + public int uniquePaths(int m, int n) { + if (m < 0 || n < 0) { + return 0; + } + return countPath(0, 0, m - 1, n - 1, new int[m][n]); + } + + private int countPath(int row, int col, int goalRow, int goalCol, int[][] memo) { + if (goalRow < row || goalCol < col) { + return 0; + } + if (row == goalRow && col == goalCol) { + return 1; + } + if (memo[row][col] != 0) { + return memo[row][col]; + } + memo[row][col] = + countPath(row + 1, col, goalRow, goalCol, memo) + + countPath(row, col + 1, goalRow, goalCol, memo); + return memo[row][col]; + } +} diff --git a/62. Unique Paths/solution1_3.java b/62. Unique Paths/solution1_3.java new file mode 100644 index 0000000..08e9c0c --- /dev/null +++ b/62. Unique Paths/solution1_3.java @@ -0,0 +1,32 @@ +/* + * ・概要 + * 自力解法 + * ボトムアップでもできるなと思い、動的計画法を使った。 + * row!=0 && col!=0のときは、dp[row][col] = dp[row - 1][col] + dp[row][col - 1]となる、(上左からしかこれないので) + * + * ・計算量 + * 時間:O(n*m) + * 空間:O(n*m) + */ + +public class solution1_3 { + public int uniquePaths(int m, int n) { + if (m < 0 || n < 0) { + return 0; + } + int[][] countOfPath = new int[m][n]; + countOfPath[0][0] = 1; + for (int row = 0; row < m; row++) { + for (int col = 0; col < n; col++) { + if (row == 0) { + countOfPath[row][col] = 1; + } else if (col == 0) { + countOfPath[row][col] = 1; + } else { + countOfPath[row][col] = countOfPath[row - 1][col] + countOfPath[row][col - 1]; + } + } + } + return countOfPath[m - 1][n - 1]; + } +} diff --git a/62. Unique Paths/solution2_1.java b/62. Unique Paths/solution2_1.java new file mode 100644 index 0000000..48bd3ef --- /dev/null +++ b/62. Unique Paths/solution2_1.java @@ -0,0 +1,35 @@ +/* + * ・概要 + * 他の人のPR参考にしたもの + * + * https://github.com/irohafternoon/LeetCode/pull/36/files#r2075900092 + * DPの考えは同じだが、一部更新部分を変えたもの。 + * その他、最初にcol == 0, row == 0のdpを1で埋める方法もある。 + * + * + * https://github.com/Fuminiton/LeetCode/pull/33/files#r2052525099 + * https://github.com/saagchicken/coding_practice/pull/19/files#r2034669315 + * w,hは領域に使いたい→同意 + * + */ + +public class solution2_1 { + public int uniquePaths(int m, int n) { + if (m < 0 || n < 0) { + return 0; + } + int[][] countOfPath = new int[m][n]; + countOfPath[0][0] = 1; + for (int row = 0; row < m; row++) { + for (int col = 0; col < n; col++) { + if (0 < row) { + countOfPath[row][col] += countOfPath[row - 1][col]; + } + if (0 < col) { + countOfPath[row][col] += countOfPath[row][col - 1]; + } + } + } + return countOfPath[m - 1][n - 1]; + } +} diff --git a/62. Unique Paths/solution2_2.java b/62. Unique Paths/solution2_2.java new file mode 100644 index 0000000..d87457f --- /dev/null +++ b/62. Unique Paths/solution2_2.java @@ -0,0 +1,26 @@ +/* + * ・概要 + * 他の人のPR参考にしたもの + * https://github.com/Fuminiton/LeetCode/pull/33/files + * 1次元配列にする方法。まあ2次元配列の方が直感的なのでよほどメモリが問題でなければ2次元のがいいなあくらいのイメージ + */ + +public class solution2_2 { + public int uniquePaths(int m, int n) { + if (m < 0 || n < 0) { + return 0; + } + int[] countOfPath = new int[n]; + countOfPath[0] = 1; + for (int row = 0; row < m; row++) { + for (int col = 0; col < n; col++) { + if (row == 0 || col == 0) { + countOfPath[col] = 1; + } else { + countOfPath[col] = countOfPath[col - 1] + countOfPath[col]; + } + } + } + return countOfPath[n - 1]; + } +}