-
Notifications
You must be signed in to change notification settings - Fork 0
62. Unique Paths #31
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
Open
miyataka
wants to merge
1
commit into
main
Choose a base branch
from
62_UniquePaths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+102
−0
Open
62. Unique Paths #31
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # 問題 | ||
| https://leetcode.com/problems/unique-paths/ | ||
|
|
||
| m x n のグリッドの左上 `grid[0][0]` にロボットがいる。ロボットは右か下にしか動けない。右下 `grid[m-1][n-1]` に到達する**経路の総数**を int で返す。 | ||
|
|
||
| - 例1: m = 3, n = 7 → 28 | ||
| - 例2: m = 3, n = 2 → 3(Right→Down→Down / Down→Down→Right / Down→Right→Down の3通り) | ||
| - 制約: 1 <= m, n <= 100。答えは 2 * 10^9 以下になるようにテストケースが作られている。 | ||
|
|
||
| # 前提 | ||
| - 答えを見ずに考えて、5分考えて分からなかったら答えを見てください。答えを見て理解したと思ったら、答えを隠して書いてください。筆が進まず5分迷ったら答えを見てください。そして、見ちゃったら一回全部消してやり直しです。答えを送信して、正解になったら、まずは一段階目です。 | ||
| - 次にコードを読みやすくするようにできるだけ整えましょう。これで動くコードになったら二段階目です。 | ||
| - そしたらまた全部消しましょう。今度は、時間を測りながら、もう一回、書きましょう。書いてアクセプトされたら文字を消してもう一回書きましょう。これを10分以内に一回もエラーを出さずに書ける状態になるまで続けてください。3回続けてそれができたらその問題はひとまず丸です。 | ||
|
|
||
| # 1回目 | ||
| ```go | ||
| // 手で解いてみた.これは,下と右にしか進めない一方向なので,上と左のマスのpathの数を足し合わせていくとよい | ||
| // これは,m*nの二次元配列を足し合わせていけば良い.端の処理があるので,番兵的に,(m+1)*(n+1)配列で(1,1)からスタートするようにしてもいいかもしれない | ||
| // 1 <= m, n <= 100 | ||
| // 答えは <= 2*10^9なのか | ||
| // 見積もり: 最大で100*100マス計算をして,それを記憶すればよいだけかもしれない. | ||
| // 10^8をGoの秒間実行ステップ数とすると | ||
| // 時間計算量としてはO(MN), 10^4 / 10^8 なので,10^-4 = 0.1ms程度. | ||
| // 空間計算量としては,10^4 * 8B = 80KB程度 | ||
| func uniquePaths(m int, n int) int { | ||
| boards := make([][]int, m) | ||
| for i := 0; i < m; i++ { | ||
| boards[i] = make([]int, n) | ||
| if i == 0 { | ||
| boards[0][0] = 1 | ||
| } | ||
| for j := 0; j < n; j++ { | ||
| var up, left int | ||
| if i != 0 { | ||
| up = boards[i-1][j] | ||
| } | ||
| if j != 0 { | ||
| left = boards[i][j-1] | ||
| } | ||
| if left + up > 0 { | ||
| boards[i][j] = left + up | ||
| } | ||
| } | ||
| } | ||
| return boards[m-1][n-1] | ||
| } | ||
| ``` | ||
| - まずは番兵なしでとりあえず解いた. | ||
| - `if left + up > 0` の条件に気づかず,だいぶ時間をロスした | ||
|
|
||
| # 2回目 | ||
| ```go | ||
| func uniquePaths(m int, n int) int { | ||
| boards := make([][]int, m+1) | ||
| boards[0] = make([]int, n+1) | ||
| for i := 1; i < m+1; i++ { | ||
| boards[i] = make([]int, n+1) | ||
| boards[1][1] = 1 | ||
| for j := 1; j < n+1; j++ { | ||
| up := boards[i-1][j] | ||
| left := boards[i][j-1] | ||
| if left + up > 0 { | ||
| boards[i][j] = left + up | ||
| } | ||
| } | ||
| } | ||
| return boards[m][n] | ||
| } | ||
| ``` | ||
| - 番兵的な解き方 | ||
| - ほかの人のコードを読む | ||
| - https://github.com/MA-yo-TA/leetcode/pull/32 | ||
| - なんか自分と全然違う解き方だな | ||
| - https://github.com/chryschron/codings/pull/31 | ||
| - 自分とすこし似ている.一つ前のindexの配列だけ保持しておけばいいので,さらに空間計算量を減らせるのか.賢いな | ||
| - https://github.com/skypenguins/coding-practice/pull/35 | ||
| - https://github.com/rimokem/arai60/pull/33 | ||
| - https://github.com/h-masder/Arai60/pull/36 | ||
| - 自分のコードの`left+up > 0`のときに分岐するのではなく,`m=0 or n=0`のときに初期化してしまうほうがわかりやすいかもしれない. | ||
| - 考え方としては,自分のほうは左上から一つずつ計算するやりかたで,`m=0 or n=0`のときに初期値をいれてしまうのは,一番上の行,一番左の列が1パターンしかないことを利用して,多少省略する考え方 | ||
| - 総合して,自分は逆に組み合わせの公式?での解法を思いつかなかった | ||
|
|
||
| # 3回目 | ||
| ```go | ||
| func uniquePaths(m, n int) int { | ||
| pathCounts := make([][]int, m+1) | ||
| for i := range pathCounts { | ||
| pathCounts[i] = make([]int, n+1) | ||
| } | ||
|
|
||
| pathCounts[0][1] = 1 // virtual input | ||
| for row := 1; row < m+1; row++ { | ||
| for col := 1; col < n+1; col++ { | ||
| pathCounts[row][col] = pathCounts[row-1][col] + pathCounts[row][col-1] | ||
| } | ||
| } | ||
| return pathCounts[m][n] | ||
| } | ||
| ``` | ||
| - 他の人のを読んでいて,pathCountsやgridのほうがいいなと思って変数名を修正. | ||
| - LLMからの初期化位置のアドバイスや,virtual inputのアイデアを利用してこれにした | ||
| - これを3回繰り返した | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
board という単語は、題材中に登場していないようです。題材中に登場する英単語や英語句を付けたほうが、読み手にとって内容を想起しやすくなると思います。 the number of possible unique paths を表す num_paths はいかがでしょうか?
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.
これはそのとおりですね.
goの流儀としてcamelCaseにするとして,numPathsかなと思いました.3回目は類似のpathCountsにしましたが,
numというprefixがより広範囲の人々に伝わりやすいかもしれないですね.コメントありがとうございます