Skip to content

Conversation

@nittoco
Copy link
Owner

@nittoco nittoco commented Jul 13, 2024

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.

nittoco added 2 commits July 14, 2024 00:39
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)かな?
Copy link

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を返す

こういうのの実装、見ると楽しいことに気づいたが、あんまりハマりすぎても問題が進まないので程々に
Copy link

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)

おそらく掛け算がビットシフトより遅いのでこういう工夫がされてる?

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

Copy link
Owner Author

@nittoco nittoco Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ面白い、速度比較のとこが特に面白かったです、ありがとうございます

### Step1

- 算数の問題?
- 小数点のずれが怖いので、最初に全部かけてから後で割った
Copy link

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

Copy link
Owner Author

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)]
Copy link

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]
Copy link

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 についても同様です。

Copy link
Owner Author

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
Copy link

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]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

末尾の要素を返せばよいため、 num_path[-1] でも良いと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants