-
Notifications
You must be signed in to change notification settings - Fork 0
Create 0062-unique-paths.md #38
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
docto-rin
wants to merge
1
commit into
main
Choose a base branch
from
0062-unique-paths
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.
Open
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,77 @@ | ||
| ## Step 1 | ||
|
|
||
| - 問題文 | ||
| - m x nのグリッドを左上から右下へ行くユニークな経路の総数を返せ。 | ||
| - 制約: | ||
| - 1 <= m, n <= 100 | ||
| - アルゴリズムの選択 | ||
| - 案1:組み合わせの総数は階乗を用いて直接計算できる。 | ||
| - 案2:進む方向が右 or 下しかないなので、m, nの小さい版の答えを利用して解ける(最適部分構造がある)ため、動的計画法 | ||
| - 案3:BFS/DFSをearly returnなしで実施し、右下に到達するたびにカウントする。 | ||
| - この順に思いついた。実装としても、この順番で好ましいと思えた。 | ||
| - 案3 については、違う経路から同じ点に来た点を別々に処理するため不利に思えた。 | ||
|
|
||
| ### 実装1 | ||
|
|
||
| - m - 1個の下矢印とn - 1個の右矢印を並べる方法の総数。 | ||
| - m + n - 2のプレースホルダーのうち下矢印を置くm - 1箇所を決めれば方法が定まるので、 | ||
|
|
||
| $${}_{m + n - 2} \mathrm{C}_{m - 1}$$ | ||
|
|
||
| - 計算量 | ||
| - Time: O(m) | ||
| - Space: O(1) | ||
|
|
||
| ```python3 | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| return math.comb(m + n - 2, m - 1) | ||
| ``` | ||
|
|
||
| - 4分かかった。(math.combを調べるのに時間がかかった) | ||
|
|
||
| ### 実装2 | ||
|
|
||
| - 動的計画法 | ||
| - 計算量 | ||
| - Time: O(mn) | ||
| - Space: O(n) | ||
|
|
||
| ```python3 | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| count = [1] * n # by column | ||
| for _ in range(1, m): | ||
| for col in range(1, n): | ||
| count[col] += count[col - 1] | ||
| return count[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. count[-1] でも良いと思います。 |
||
| ``` | ||
|
|
||
| - 0行目、0列目の境界条件に少し頭を使い、9分かかった。 | ||
| - セルフfollow-up: m, nのどちらかが非常に大きい場合、そのままにしますか? | ||
| - 空間計算量の節約のため、m > nになるようにswapできる。時間計算量はほぼ不変なのでお得。 | ||
| - n, m = sorted([m, n]) | ||
|
|
||
| ## Step 2 | ||
|
|
||
| - [コメント集](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.brtd7l7oqr0f) | ||
| - https://discord.com/channels/1084280443945353267/1339428945845555252/1360645783300341760 | ||
| - > あー、このコード動くのかと思ったが、そうか、これ動くのか。1次元テーブルでの解法になっていますね。 | ||
| - oda-sanの反応に思わず笑ってしまった。 | ||
| - 実質的に[実装2](#実装2)が行われている。 | ||
| - list * n は常にlistの中身の参照を複製している(shallow copy)。 | ||
| - 要素が mutable(リストなど)のとき | ||
| - 共有しているオブジェクトをある一箇所で変更すると、全ての位置から同じ変更が見える状態。 | ||
| - -> 全ての要素が変更される。 | ||
| - 要素が immutable(intなど)のとき | ||
| - 共有しているオブジェクトをある一箇所で変更すると、要素がimmutableゆえオブジェクトが新規作成(参照が変更)され、他からは見えない。 | ||
| - -> 別々に変更ができる。 | ||
| - なので、2次元テーブルの準備には、ミュータブルの複製のためにforループを使ってオブジェクトを明示的に新規作成する。 | ||
| - table = [[0] * 10 for _ in range(10)] | ||
|
|
||
| ## Step 3 | ||
|
|
||
| [#実装1](#実装1) | ||
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.
気が向いたら math.comb を読んでもいいでしょう。(ライブラリーではどのような使われ方がするかが分からないのでとてもチューニングがされる傾向があります。)