Conversation
| count = [[0] * n for _ in range(m)] | ||
| for row in range(m): | ||
| for col in range(n): | ||
| if row == 0 or col == 0: |
There was a problem hiding this comment.
読み直すと、上端か左端かの2つのチェックが一行にまとまっているのがちょっとわかりづらい?
step1.py のように初期化する方が素直かも。
There was a problem hiding this comment.
個人的にはこちらの方が読みやすいです.できるだけ同じ枠組みに入れてしまうのが好きと言うのもあるかもしれません.
There was a problem hiding this comment.
ありがとうございます、他の方々との感覚のすり合わせができて助かります!
There was a problem hiding this comment.
僕は前者の方が目的が明確で読みやすく感じましたが、好みのレベルだと思います。どちらも読みやすいです!
| 素直に `m * n` の grid を用意して埋めていけばいい。(`step1.py`) | ||
| 時間計算量: O(mn), 空間計算量: O(mn) | ||
|
|
||
| `m * n` 全ての結果を持つ必要はなくて、次のrowを埋めるのに必要なのは直前のrowだけなので、2つのrowだけ持てばいい。(`step1_two_rows.py`) |
There was a problem hiding this comment.
さらに言えば,rowの更新は左から右の一方向なので1つのrowを持てば十分ですね.つまりcurrent_row[col]を更新する直前,current_row[col]にはprev_row[col]と同じ値が入っているはずなので
current_row[col] += current_row[col - 1]と更新できます.(可読性は直前と今で別変数に入れた方が良いでしょうが)
|
|
||
|
|
||
| class Solution: | ||
| @functools.cache |
| count = [[0] * n for _ in range(m)] | ||
| for row in range(m): | ||
| for col in range(n): | ||
| if row == 0 or col == 0: |
There was a problem hiding this comment.
個人的にはこちらの方が読みやすいです.できるだけ同じ枠組みに入れてしまうのが好きと言うのもあるかもしれません.
|
|
||
| [dxxsxsxkxさんのPR](https://github.com/dxxsxsxkx/leetcode/pull/33/changes#diff-e425ab53cee19df2ef6995889ba4e527fc0e29e480a6787c15119f2da066511c) | ||
|
|
||
| 動的計画法だと初めからわかっていたのでそれに飛びついてしまったが、いつかの数学でやった組み合わせの問題であることも連想できた方が良かったな。 |
There was a problem hiding this comment.
これは僕も同じことしました。
こういうやり方もあるよくらいは言えた方が良さそうですよね。
| count = [[0] * n for _ in range(m)] | ||
| for row in range(m): | ||
| for col in range(n): | ||
| if row == 0 or col == 0: |
There was a problem hiding this comment.
僕は前者の方が目的が明確で読みやすく感じましたが、好みのレベルだと思います。どちらも読みやすいです!
https://leetcode.com/problems/unique-paths/description/