-
Notifications
You must be signed in to change notification settings - Fork 0
62. Unique Paths #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
62. Unique Paths #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. == 0 もいれていいんではないでしょうか。そうしないと下で [0][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]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* ・概要 | ||
* 他の人のPR参考にしたもの | ||
* | ||
* https://github.com/irohafternoon/LeetCode/pull/36/files#r2075900092 | ||
* DPの考えは同じだが、一部更新部分を変えたもの。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. この更新方法が個人的には一番好きです |
||
* その他、最初に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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一応、配列の範囲外アクセスも考慮して、m=0 or n=0もここで一緒に弾いてもいいと思います。(今回はm>=1、n>=1とされていますが) |
||
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]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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++) { | ||
Comment on lines
+15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rowとcolを1から回しても大丈夫そうですね |
||
if (row == 0 || col == 0) { | ||
countOfPath[col] = 1; | ||
} else { | ||
countOfPath[col] = countOfPath[col - 1] + countOfPath[col]; | ||
} | ||
} | ||
} | ||
return countOfPath[n - 1]; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 分かりやすいです。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分ならここで<=0にして、L20~21の条件分岐を減らすかもしれません