-
Notifications
You must be signed in to change notification settings - Fork 0
62. Unique Paths #35
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 #35
Conversation
hayashi-ay
left a comment
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.
良いと思います
|
|
||
| 組み合わせを計算 | ||
| ```python | ||
| from scipy.special import comb |
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じゃなくてscipyのcombを使っているのはなにか理由があるんですか?
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を使っていたのでバラエティをと思いまして。
mathのcombはpython3.8から追加されたようですね。
今回の話とは関係ないですが、scipyのcombは重複順列を求めるオプション(repetition)があるようです。
| return grid[m - 1][n - 1] | ||
| ``` | ||
| 思考ログ: | ||
| - 小学生の時にやったマスの上と左に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.
自分も思いました
| - 小学生の時にやったマスの上と左に1を書いて足していくやつ | ||
| - 問題文につられて```grid```にしたが、```num_ways```とかにした方が情報があって良かったか | ||
| - ある座標[i, j]への行き方の総数を記録した二次元配列 | ||
| - ```grid```の作成と壁際の初期化を同時にやってしまったが、冗長でも分けても良かったかもしれない |
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.
これ、私は分けるのに賛成ですね。(趣味の範囲。)
口頭で説明する場合は、「m * n を作って、0 列目を1で埋め、0行目を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.
念の為、
grid = [[0] * n] * mが違うものは出来上がるということをコメントしておきます。
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/library/copy.html
| - https://github.com/nittoco/leetcode/pull/26 | ||
| - 再帰 | ||
| - mathモジュールの話題 | ||
| - https://github.com/nittoco/leetcode/pull/26/files#r1677082520 |
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.
あ、コードを読むのは書くのよりも大事なのでどこかからはやるようにして欲しいです。
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.
はい。一応全ての過去ログ(PR)にはリンク先も含め目を通しているのですが、なかなか時間がかかってしまってます。。
読み書きの比率が読みに偏っていること自体は問題ないのだと思うのですが、正直なところを白状すると、いまだに人のコードを読んだりドキュメントを読んだりということが自然に出来ている感覚がなく、(推奨されているから)頑張ってやっている、感が少なからずあります。
これは単純にやり方が悪いのか、マインドセットの問題なのか、解消の糸口が見つからずにいます。
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.
うーん、PR はともかくプロダクションのコードは誰かが誰かに使ってもらうために作っているものである、という面はありますね。
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.
コードを読むでいうと、目的があって、それを確かめるために読む側面があるので、全部目を通すということにこだわるよりかは自分の気になるところだけまずは読んでみるのが良いのかなとも思います。
n-1とm-1の小さい方を入れた方が効率がいいかなと思ったが、実装を見たら(_comb_intの中で)やってるようなので気にしないことに
たとえば、これとかはうまく実践できている例だなと自分は感じました。
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.
そうですね〜、自分も仕事でコード書くときに、急いでたりするとなかなかドキュメントが見れないでいます(データ分析だと、プロダクションコードじゃないので意識が希薄になってしまいがちというのもあるのかも)
仕事だとLeetCodeよりいろんなライブラリのメソッドを使うので、LeetCodeよりさらに多くのドキュメント見る必要があり、難しいですよね。3つに1つくらいはせめて見るようにしたいです、、、
| num_ways = [1] * n | ||
| for row_i in range(1, m): | ||
| for col_i in range(1, n): | ||
| num_ways[col_i] = num_ways[col_i] + num_ways[col_i - 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.
step3 のように、 num_ways[col_i] += num_ways[col_i - 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.
ありがとうございます。
他の方のPRで同様のご指摘を拝見して、STEP3で修正しました。
| class Solution: | ||
| def uniquePaths(self, m: int, n: int) -> int: | ||
| # 壁際のマスへの行き方は1通りしかないのでgridの初期化の際に1で埋める | ||
| grid = [] |
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.
grid = [[0] * n for _ in range(m)]
grid[0][0] = 1
として、 row_i と col_i が 0 でない場合は加算するという書き方もありますが、どちらがシンプルかは微妙なところです。
| def uniquePaths(self, m: int, n: int) -> int: | ||
| # 壁際のマスへの行き方は1通りしかないのでgridの初期化の際に1で埋める | ||
| grid = [] | ||
| for row_i in range(m): |
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.
suffix の _i は何を表していますか?もし index を表しているのであれば、単に row/col でも十分に意味は伝わると思いますので、消してよいと思います。
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.
indexです。
私も使いながらモヤモヤしていたのですが、今後はrow/colでいこうと思います。
ありがとうございます。
| num_ways = [1] * n | ||
| for _ in range(1, m): | ||
| for i in range(1, n): | ||
| num_ways[i] += num_ways[i - 1] | ||
|
|
||
| return num_ways[-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.
個人的にはいきなり1で埋めるのはちょっと抵抗感あるかもという感じで、
num_ways = [0] * n
num_ways[0] = 1
for _ in range(m):
for i in range(1, n):
num_ways[i] += num_ways[i - 1]の方がいいかなと思ったんですが、難しいですね
https://leetcode.com/problems/unique-paths/description/