-
Notifications
You must be signed in to change notification settings - Fork 0
62. Unique Paths #26
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 #26
Conversation
URL: https://leetcode.com/problems/unique-paths/description/ There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. The test cases are generated so that the answer will be less than or equal to 2 * 109.
| ## Step2 | ||
|
|
||
| - 再帰を試したらTLE、再帰の回数が多すぎ | ||
| - 呼び出し回数、2^(m+n)かな? |
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.
これの計算量、最終的にすべて1を足しているはずなので、返ってくる値と同じです。
| - k = min(n-k, k)にする。そのあとkがoverflowしないかもチェック | ||
| - n < kなら0を返す | ||
|
|
||
| こういうのの実装、見ると楽しいことに気づいたが、あんまりハマりすぎても問題が進まないので程々に |
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モジュールの階乗のコードを見る(https://github.com/python/cpython/blob/main/Modules/mathmodule.c) | ||
|
|
||
| おそらく掛け算がビットシフトより遅いのでこういう工夫がされてる? |
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.
そうです。テクニック周りの話がまとまっている記事があるので、参考になりそうです。この例はfactorial関数についてですが、だいたい同じだと思います。
https://qiita.com/AkariLuminous/items/1b2e964ebabde9419224
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.
これ面白い、速度比較のとこが特に面白かったです、ありがとうございます
| ### Step1 | ||
|
|
||
| - 算数の問題? | ||
| - 小数点のずれが怖いので、最初に全部かけてから後で割った |
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.
// //= 演算子を使えば整数で割り算ができると思います。
https://docs.python.org/ja/3/reference/lexical_analysis.html#operators
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.
すみません、わかりにくかったですが、C(5, 2)の時、5//2 = 2 2*4 = 8 8//1 = 8で本来10なのに切り捨てられてずれるのが怖いということでした。
割るのを1からにしたらずれませんね
| ```python | ||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| num_path = [[0] * m for _ in range(n)] |
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.
path は 2 個以上ある場合がありますので、複数形にして num_paths とするとよいと思います。
| if row == 0 or column == 0: | ||
| num_path[row][column] = 1 | ||
| continue | ||
| num_path[row][column] = num_path[row - 1][column] + num_path[row][column - 1] |
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.
row が 0 のとき row - 1 が -1 となり、末尾の要素を参照している点に違和感を感じました。 row が 0 以上の場合にのみ処理をしたほうが良いと思います。 column についても同様です。
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.
これはrow == 0 or col == 0でcontinueしてるので大丈夫だと思います
|
|
||
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| num_path = [0] * n |
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_path[0] = 1 と代入しておき、 for i in range(1, n): とすると、 if 文が消せると思います。
| num_path[row] = 1 | ||
| continue | ||
| num_path[row] += num_path[row - 1] | ||
| return num_path[n - 1] |
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_path[-1] でも良いと思います。
URL: https://leetcode.com/problems/unique-paths/description/
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * 109.