-
Notifications
You must be signed in to change notification settings - Fork 0
62. Unique Paths #34
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
fhiyo
wants to merge
1
commit into
main
Choose a base branch
from
62_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
62. Unique Paths #34
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,135 @@ | ||
| # 62. Unique Paths | ||
|
|
||
| ## 1st | ||
|
|
||
| ### ① | ||
|
|
||
| grid全部を持たなくても1行 or 1列のみ持ってそれを開始地点側から到着地点側へ移動させていけば良い。 | ||
| 今回はrowを取り回しているがrowとcolumnで短い方を取り回すようにすれば空間計算量はO(min(m,n))になる。 | ||
|
|
||
| 変数名、rowではないな。num_pathsとかか。 | ||
|
|
||
| 所要時間: 4:56 | ||
|
|
||
| - 時間計算量: O(mn) | ||
| - 空間計算量: O(n) | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| row = [1] * n # first row's unique path count | ||
| for _ in range(1, m): | ||
| for i in range(1, n): | ||
| row[i] += row[i - 1] # sum unique path count from left and up | ||
| return row[-1] | ||
| ``` | ||
|
|
||
| ### ② | ||
|
|
||
| 再帰。(row, col)の位置からゴールに行くためのpathの数を再帰的に計算する。 | ||
| cacheを使うと無制限にデータを貯めてしまうので少し抵抗がある。今回の問題はgridの大きさは大したことにならないはずなのでいいかもしれないが。 | ||
|
|
||
| 所要時間: 5:58 | ||
|
|
||
| - 時間計算量: O(mn) | ||
| - 空間計算量: O(mn) | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| @cache | ||
| def count_paths_from(row: int, col: int) -> int: | ||
| if row == m - 1 and col == n - 1: | ||
| return 1 | ||
| count = 0 | ||
| if row < m - 1: | ||
| count += count_paths_from(row + 1, col) | ||
| if col < n - 1: | ||
| count += count_paths_from(row, col + 1) | ||
| return count | ||
|
|
||
| return count_paths_from(0, 0) | ||
| ``` | ||
|
|
||
| ## 2nd | ||
|
|
||
| ### 参考 | ||
|
|
||
| - https://discord.com/channels/1084280443945353267/1233603535862628432/1255522219258286240 | ||
|
|
||
| gridの短い方の辺を取り回すように書く。 | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| if m < n: | ||
| m, n = n, m | ||
| num_paths = [1] * n # first row's number of paths | ||
| for _ in range(1, m): | ||
| for i in range(1, n): | ||
| num_paths[i] += num_paths[i - 1] # sum number of paths from left and up | ||
| return num_paths[-1] | ||
|
|
||
| ``` | ||
|
|
||
| nCkで解く。 | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| return comb(m + n - 2, m - 1) | ||
| ``` | ||
|
|
||
| combのソース: https://github.com/python/cpython/blob/17d5b9df10f53ae3c09c8b22f27d25d9e83b4b7e/Modules/mathmodule.c#L3805 ざっとだが読んだ。 | ||
| そもそも任意精度をPythonでどう表現しているのかが気になったので、調べて出てきた記事 [Pythonの整数型はどのように実装されているのか](https://zenn.dev/yukinarit/articles/afb263bf68fff2) を読んだ。PyObjectの中でob_sizeとob_digitという変数を使って2^30の値を配列で持つことで大きな数を表現しているらしい。 | ||
|
|
||
| combの簡易的な自作。 | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| @cache | ||
| def comb_(n: int, k: int) -> int: | ||
| if k == 0: | ||
| return 1 | ||
| if k == 1: | ||
| return n | ||
| j = k // 2 | ||
| return comb_(n, j) * comb_(n - j, k - j) // comb_(k, j) | ||
|
|
||
| return comb_(m + n - 2, n - 1) | ||
| ``` | ||
|
|
||
| - https://discord.com/channels/1084280443945353267/1192736784354918470/1250055847225069631 | ||
| - https://discord.com/channels/1084280443945353267/1227073733844406343/1243465298850349089 | ||
|
|
||
| nCk = n-1Ck + n-1Ck-1 があったか。 | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| @cache | ||
| def comb_(n: int, k: int) -> int: | ||
| if n == 0 or k == 0 or n == k: | ||
| return 1 | ||
| if k == 1: | ||
| return n | ||
| return comb_(n - 1, k) + comb_(n - 1, k - 1) | ||
|
|
||
| return comb_(m + n - 2, n - 1) | ||
| ``` | ||
|
|
||
|
|
||
| ## 3rd | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| if m < n: | ||
| m, n = n, m | ||
| num_paths = [1] * n # first row's number of paths | ||
| for _ in range(1, m): | ||
| for i in range(1, n): | ||
| num_paths[i] += num_paths[i - 1] # sum number of paths from left and up | ||
| return num_paths[-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.
コメントにもある通り、
num_pathsのほうが良いと思います。